Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- public class eje3 {
- public static void main(String[] args) throws IOException {
- Scanner teclado = new Scanner(System.in);
- int contador = 0;
- String nombres[] = new String[50];
- System.out.println("Introduzca los nombres a introducir en el fichero. Introduzca un \"#\" para terminar:");
- escritura(teclado, contador, nombres);
- leecadenas();
- leecaracteres();
- }
- private static void leecaracteres() {
- try {
- File f = new File("Nombres.txt");
- if (f.exists()) {
- FileReader fr = new FileReader(f);
- BufferedReader br = new BufferedReader(fr);
- System.out.println("\nLectura por carácteres:");
- int caracter;
- while ((caracter = br.read()) != -1) {
- System.out.println((char) caracter);
- }
- br.close();
- }
- } catch (FileNotFoundException fn) {
- System.out.println("No se encuentra el fichero");
- } catch (IOException ioe) {
- System.out.println("Error de L/E");
- }
- }
- private static void leecadenas() {
- try {
- File f = new File("Nombres.txt");
- if (f.exists()) {
- FileReader fr = new FileReader(f);
- BufferedReader br = new BufferedReader(fr);
- System.out.println("\nLectura por cadenas:");
- String nombre;
- while ((nombre = br.readLine()) != null) {
- System.out.println(nombre);
- }
- br.close();
- }
- } catch (FileNotFoundException fn) {
- System.out.println("No se encuentra el fichero");
- } catch (IOException ioe) {
- System.out.println("Error de L/E");
- }
- }
- private static void escritura(Scanner teclado, int contador, String[] nombres) throws IOException {
- String cadena;
- do {
- cadena = teclado.next();
- if (!cadena.equals("#")) {
- nombres[contador] = cadena;
- contador++;
- }
- } while (!cadena.equals("#"));
- FileWriter fw = new FileWriter("Nombres.txt");
- PrintWriter salida = new PrintWriter(fw);
- for (int i = 0; i < contador; i++) {
- salida.println(nombres[i]);
- }
- salida.flush();
- salida.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement