Advertisement
Grela

Eje3

Sep 22nd, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9.  
  10. public class eje3 {
  11.     public static void main(String[] args) throws IOException {
  12.         Scanner teclado = new Scanner(System.in);
  13.         int contador = 0;
  14.         String nombres[] = new String[50];
  15.  
  16.         System.out.println("Introduzca los nombres a introducir en el fichero. Introduzca un \"#\" para terminar:");
  17.         escritura(teclado, contador, nombres);
  18.  
  19.         leecadenas();
  20.  
  21.         leecaracteres();
  22.     }
  23.  
  24.     private static void leecaracteres() {
  25.         try {
  26.             File f = new File("Nombres.txt");
  27.             if (f.exists()) {
  28.                 FileReader fr = new FileReader(f);
  29.                 BufferedReader br = new BufferedReader(fr);
  30.  
  31.                 System.out.println("\nLectura por carácteres:");
  32.                 int caracter;
  33.                 while ((caracter = br.read()) != -1) {
  34.                     System.out.println((char) caracter);
  35.                 }
  36.                 br.close();
  37.             }
  38.         } catch (FileNotFoundException fn) {
  39.             System.out.println("No se encuentra el fichero");
  40.         } catch (IOException ioe) {
  41.             System.out.println("Error de L/E");
  42.         }
  43.     }
  44.  
  45.     private static void leecadenas() {
  46.         try {
  47.             File f = new File("Nombres.txt");
  48.             if (f.exists()) {
  49.                 FileReader fr = new FileReader(f);
  50.                 BufferedReader br = new BufferedReader(fr);
  51.  
  52.                 System.out.println("\nLectura por cadenas:");
  53.                 String nombre;
  54.                 while ((nombre = br.readLine()) != null) {
  55.                     System.out.println(nombre);
  56.                 }
  57.                 br.close();
  58.             }
  59.         } catch (FileNotFoundException fn) {
  60.             System.out.println("No se encuentra el fichero");
  61.         } catch (IOException ioe) {
  62.             System.out.println("Error de L/E");
  63.         }
  64.     }
  65.  
  66.     private static void escritura(Scanner teclado, int contador, String[] nombres) throws IOException {
  67.         String cadena;
  68.         do {
  69.             cadena = teclado.next();
  70.             if (!cadena.equals("#")) {
  71.                 nombres[contador] = cadena;
  72.                 contador++;
  73.             }
  74.         } while (!cadena.equals("#"));
  75.  
  76.         FileWriter fw = new FileWriter("Nombres.txt");
  77.         PrintWriter salida = new PrintWriter(fw);
  78.  
  79.         for (int i = 0; i < contador; i++) {
  80.             salida.println(nombres[i]);
  81.         }
  82.  
  83.         salida.flush();
  84.         salida.close();
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement