resu
resu

Reputation: 321

BufferedReader readLine used in while loop

I have seen BufferedReader using while loops to iterate through the contents of a file, with code more or less like:

try {
   FileReader fr = new FileReader(file);
   Buffered Reader br = new BufferedReader(fr);
   String line;

   while ( (line = br.readLine()) != null ) {
      // do something
   } 
} catch () {}

what I don't understand is how the while loop is internally incrementing its counter until it has read all lines in the document. To me, the while loop above means "if the first line (line[0]) is not null, do something (presumably an infinite number of times)", and never advancing past the first line of the document. What am I not understanding about BufferedReader or the .readLine() method?

Upvotes: 3

Views: 7764

Answers (4)

adsdf
adsdf

Reputation: 166

I hope I get your question right. You would like to know how BufferedReader determines where to continue reading within the loop without a counting variable?

If you take a look inside BufferedReader.class you will see a private int pos; counter that is incremented every time a char is read from the stream, e.g. in public int read(). Same is happening in readLine() with the difference that pos is incremented until the end of the line is reached.

You can reset the internal counter using reset() function (this is to the last mark location, see here for details).

Upvotes: 3

Eric J.
Eric J.

Reputation: 150108

Examine the code

while ( (line = br.readLine()) != null ) {
  // do something
} 

First line is assigned to whatever br.readLine() returns, then line is compared to null. The parenthesis enforce that order of operations.

The variable line will keep taking on the value of the text of the next line as the program loops through all lines in the file, until it reaches the end of the file. At that point line is assigned the value null, which then terminates the loop.

what I don't understand is how the while loop is internally incrementing its counter

There is no counter for this loop. The loop termination condition is line == null (another way of saying it keeps looping while line != null). The loop ends when line is null.

To me, the while loop above means "if the first line (line[0]) is not null

No, line is not an array. It is a string representing a single line of the file, and that value gets updated as each line is read from the file.

In .NET there is a different method

string[] allLines = File.ReadAllLines(path);

That different approach reads all lines of the file into memory at once. That method is handy for reading in small files, while the method you present is much more memory efficient. It streams the contents of the file, only allocating memory for the current line. That approach is far superior for large files.

Note that the buffered reader plays no role in the loop semantics. It is simply a mechanism to more efficiently read the file from disk (or perhaps unnecessary overhead).

Upvotes: 0

M.Usman Siddiqui
M.Usman Siddiqui

Reputation: 79

firstly, Its necessary to understand the difference between Filereader and Buffered Reader. Buffered Reader reads text from a character-input stream,buffering characters so as to provide for the efficient reading of characters,arrays and lines. Whereas FileReader reads a line of text.A line is considered to be terminated by one of the line feeds or a return. Just Keep in mind that While would execute when the first line in a file is not empty.Even if there is a single dot there it would read it. Hope It helps Now. :) Comment below for further details. Have a nice day.

Upvotes: 0

akhil_mittal
akhil_mittal

Reputation: 24157

The line inside braces of the following while loop:

while ( (line = br.readLine()) != null )

is

(line = br.readLine()) != null

And the method readLine() will keep on reading the next line from the file and once it reaches the end of the file it returns null. So it works this way.

On a side note there is no restriction of a counter for while loop, only thing it needs is an boolean expression that evaluates to true or false.

Upvotes: 0

Related Questions