Java - ayuda con FileReader

 
Vista:

ayuda con FileReader

Publicado por israel (1 intervención) el 29/06/2019 03:42:32
Que tal amigos, tengo una consulta, estoy utilizando FileReader para acceder al contenido de un archivo .txt , el cual si lo lee pero no lee todo el contenido del texto, solo apartir de la mitad aproximadamente, a continuacion ingreso el codigo esperando alguien me pueda orientar.
gracias.
Adjunto el archivo "Romeo.txt" el cual lo guarde en la carpeta del proyecto eclipse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static void main(String args[]) {
    String texto= "";
    try {
        BufferedReader br;
 
        FileReader fr =new FileReader("Romeo.txt");
        br= new BufferedReader(fr);
        System.out.println("El texto contenido en el archivo es: ");
 
        String linea=br.readLine();
        while(linea !=null) {
            System.out.println(linea);
            linea=br.readLine();
        }
        br.close();
    }catch(IOException ioe) {System.out.println("\n\nError al abrir o guardar el archivo: ");
            ioe.printStackTrace();
    }catch(Exception e) {
        System.out.println("\n\nError al leer de teclado: ");
        e.printStackTrace();
    }
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Billy Joel
Val: 2.665
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

ayuda con FileReader

Publicado por Billy Joel (876 intervenciones) el 29/06/2019 17:49:40
Prueba con este código:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public static void main(String[] arg) {
    File archivo;
    FileReader fr = null;
    BufferedReader br;
 
    try {
        archivo = new File("Romeo.txt");
        fr = new FileReader(archivo);
        br = new BufferedReader(fr);
 
        // Lectura del fichero
        String linea;
        while ((linea = br.readLine()) != null) {
            System.out.println(linea);
        }
    } catch (IOException e) {
        e.printStackTrace(System.out);
    } finally {
        try {
            if (null != fr) {
                fr.close();
            }
        } catch (IOException e2) {
            e2.printStackTrace(System.out);
        }
    }
}

Saludos,
Billy Joel
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar