开发者

BufferedReader ready method in a while loop to determine EOF?

I have a large file (English Wi开发者_Go百科kipedia 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.


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) {
    ...
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜