Query regarding reading a text file (Module 7) | Selenium Forum
Himanshi Posted on 07/02/2020

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)

while(bfr.readLine() != null)

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?

Ashish 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.

Related Posts