-1

if you have something like this

FileReader fileReader = 
            new FileReader(fileName);


BufferedReader bufferedReader = 
            new BufferedReader(fileReader);

while((line = bufferedReader.readLine()) != null) {
    System.out.println(line);
}   

Why does bufferedeader.readline() read the next line after the first one? What's confusing to me is that there isn't a readnextline method and I don't understand why readline would continue reading the rest of the file instead of looping the first line infinitely.

M A
  • 71,713
  • 13
  • 134
  • 174
Jnewbie
  • 31
  • 10
  • It does this so it's easier to read the entire file. – sguan Oct 18 '15 at 18:18
  • According to the second part of your question: (usually) reading from a stream (or reader) actually removes the characters from the stream. You can read each byte or character only once. – Robert Reiner Oct 18 '15 at 18:25

1 Answers1

1

You can rewrite this to:

line = bufferedReader.readLine()
while (line != null) {
  ... print ...
  line = bufferedReader.readLine();

That should answer you question ... (the point being the fact that readLine(); well, reads ONE line; after the other; and returns null if there wasnt any more line to read)

GhostCat
  • 137,827
  • 25
  • 176
  • 248