• Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferedReader - Reading a file  RSS feed

 
Jason Venter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen you can do the following to read a file line by line with FileReader and BufferedReader:



Why do you need to store the value in the "line" variable? I tried removing it and only using bufferedReader.readLine() in the while condition and sysout statement:



However the entire file is not printed, just a few lines.

For example, here is my testfile.txt:

This
is
a
test
file

Here is the output after running readFile("test.txt"); from main:

is
test
null

Not sure why this result is so different. Is it because every time I call readline(), it moves the file cursor up 1?

 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure why this result is so different. Is it because every time I call readline(), it moves the file cursor up 1?

I think this is correct. Your readLine in the conditional of the while returns a line and moves the cursor. Just because you don't put the result in a variable doesn't change that.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!