Please look into the below code:
import java.io.*;
FileReader fr = new FileReader("D:\\JAVA_Projects\\test1.txt");
BufferedReader bfr = new BufferedReader(fr);
/*String x;
while((x=bfr.readLine()) != null)
{
System.out.println(x);
}*/
while(bfr.readLine() != null)
{
System.out.println(bfr.readLine());
}
}
In the above code commented while loop to print the multiple lines from a file is giving me the right output.
But if I am executing uncommented while loop it is printing only the even num line. There are total 11 lines ina text file.
Attached a screenshot of both the loop.
I want to ask why is it necessary to assign the value of bfr.readline() method to a variable x of string type, why can't I write it directly?
Instructor
Ashish Thakur Replied on 11/02/2020
Please cross-check the code. As i can see that you have not used x variable.
You need to read and store data in a variable and then need to access it. If you use the method used by you, this will call for another set of data every time when you mention .readLine() method.