Advertisement
Namaru

Lectura, escritura de ficheros JAVA.

May 18th, 2024 (edited)
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | Source Code | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4.  
  5. public class LecturaEscritura_FicherosPlanos {
  6.  
  7.     private static final Scanner scn = new Scanner(System.in);
  8.  
  9.     public void escritura(String linea) {
  10.         try {
  11.             FileWriter fichero = new FileWriter("texto.txt", true);
  12.             BufferedWriter flujo = new BufferedWriter(fichero);
  13.             PrintWriter escribir = new PrintWriter(flujo);
  14.             escribir.println(linea);
  15.             escribir.flush();
  16.         } catch (IOException e) {
  17.             System.err.println("Error: " + e.getMessage());
  18.         }
  19.     }
  20.  
  21.     public ArrayList<String> leer() {
  22.         ArrayList<String> texto = new ArrayList<>();
  23.  
  24.         try {
  25.             FileReader fichero = new FileReader("texto.txt");
  26.             BufferedReader leer = new BufferedReader(fichero);
  27.  
  28.             while (leer.ready()) {
  29.                 texto.add(leer.readLine());
  30.             }
  31.         } catch (IOException e) {
  32.             System.err.printf("Error leer: ", e.getMessage());
  33.            
  34.         }
  35.         return texto;
  36.     }
  37.  
  38.     /**
  39.      * @param args the command line arguments
  40.      */
  41.     public static void main(String[] args) {
  42.         LecturaEscritura_FicherosPlanos fichero = new LecturaEscritura_FicherosPlanos();
  43.         // TODO code application logic here
  44.         String linea;
  45.         System.out.println("Elija escribir 'E' o Leer 'L'");
  46.         linea = scn.nextLine();
  47.         if (linea.toUpperCase().equals("E")) {
  48.             scn.reset();
  49.             System.out.println("Escriba");
  50.             linea = scn.nextLine();
  51.             fichero.escritura(linea);
  52.  
  53.         } else if (linea.toUpperCase().equals("L")) {
  54.             //System.err.printf("Datos en fichero %-51s", linea);
  55.             String[] texto = fichero.leer().toArray(args);
  56.             LinkedList<String> list = new LinkedList<>();
  57.  
  58.             for (String texto1 : texto) {
  59.                 int longitud = texto1.length();
  60.                 int cont = longitud;
  61.                 String text;
  62.                 int num = 0;
  63.                 do {                    
  64.                     if (longitud > 50) {
  65.                         int rest = longitud - cont;        
  66.                         int tern = ((cont<51)?50*num+cont:50*num+50);                        
  67.                         //si es mayor de 50 la longitud hace salto de linea y continua con el mismo.                        
  68.                         text = texto1.substring( rest, tern ) + ((texto1.charAt((tern-1))!=' '&& cont > 50)?"-":"");
  69.                         System.out.printf("| %-" + 51 + "s", text).println("|" /*+ cont*/);
  70.                         //contadores:
  71.                         cont -= 50;                        
  72.                         num += 1;
  73.                     } else {
  74.                         cont = 0;
  75.                         System.out.printf("| %-" + 51 + "s", texto1).println("|" /*+ longitud*/);
  76.                     }                    
  77.                 } while (cont > 0);                
  78.             }
  79.         }
  80.     }
  81.  
  82. }
Tags: Java simple
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement