Read several lines of a JAVA text file

1

I need to be able to read several lines of a .txt with a specific format per line (String, int, int) for example (Erick, 99,88) .

After that, make a split of each line, using as a parameter for the split a comma, to be able to operate with the two whole numbers of each line. I went ahead with the following code, but I can only print the 2nd line and I have no idea why.

File file = new File("Calificaciones.txt");

FileReader fileR = null;
BufferedReader file2 = null;

try {
    fileR = new FileReader(file);
    file2 = new BufferedReader(fileR);


} catch (FileNotFoundException e) {
    System.out.println("No se encontro el archivo "+file.getName());
}

try {
    while(file2.readLine() != null)
        {
            String lines = file2.readLine();
            System.out.println(lines);
        }
} catch (IOException e) {
    e.printStackTrace();
}
    
asked by ErickLugoJ 19.05.2017 в 03:23
source

2 answers

3

Try if this modification works for you so they are in all the examples: Read files in java

File file = new File("Calificaciones.txt");
FileReader fileR = null;
BufferedReader file2 = null;

try {
    fileR = new FileReader(file);
    file2 = new BufferedReader(fileR);


} catch (FileNotFoundException e) {
    System.out.println("No se encontro el archivo "+file.getName());
}

try {
    String lines = "";
    while( ( lines = file2.readLine()) != null) {
        System.out.println(lines);
    }
} catch (IOException e) {
    e.printStackTrace();
}
    
answered by 19.05.2017 / 04:25
source
1

Just change the part of the While and now write it in the following way '

String lines;
while((lines=file2.readLine())!=null)
            {

                System.out.println(lines);
            }
    
answered by 19.05.2017 в 04:22