Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedReader ready method in a while loop to determine EOF?

I have a large file (English Wikipedia articles only database as XML files). I am reading one character at a time using BufferedReader. The pseudocode is:

file = BufferedReader...

while (file.ready())
    character = file.read()

Is this actually valid? Or will ready just return false when it is waiting for the HDD to return data and not actually when the EOF has been reached? I tried to use if (file.read() == -1) but seemed to run into an infinite loop that I literally could not find.

I am just wondering if it is reading the whole file as my statistics say 444,380 Wikipedia pages have been read but I thought there were many more articles.

like image 664
BobTurbo Avatar asked Dec 28 '10 04:12

BobTurbo


2 Answers

The Reader.ready() method is not intended to be used to test for end of file. Rather, it is a way to test whether calling read() will block.

The correct way to detect that you have reached EOF is to examine the result of a read call.

For example, if you are reading a character at a time, the read() method returns an int which will either be a valid character or -1 if you've reached the end-of-file. Thus, your code should look like this:

int character;
while ((character = file.read()) != -1) {
    ...
}
like image 85
Stephen C Avatar answered Sep 28 '22 03:09

Stephen C


This is not guaranteed to read the whole input. ready() just tells you if the underlying stream has some content ready. If it is abstracting over a network socket or file, for example, it could mean that there isn't any buffered data available yet.

like image 23
Mike Samuel Avatar answered Sep 28 '22 03:09

Mike Samuel