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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With