OK, so I have this program, which reads two characters, appends them to a StringBuffer, adds the resulting string to a Vector, and does the same until the end of file is reached.
Now, since I didn't managed to remember or find anything on the documentation about an End Of File flag (I remember you can see if you reached it on C with the feof(An input stream) function), I found that I can use the available() function from the FileInputStream class, which returns how much bytes are between the file pointer and the EOF marker. So, I have a While loop like this:
The problem? This code, compiled with JCreator 3.50 LE, worked yesterday in my computer, but it doesn't works anymore! When I run my program, I get the following stack dump (BTW, I think "Controlador no v�lido" translates to "Invalid controller" or something; I've never seen the English version since my computer is in Spanish):
Any idea on what could be going on? [ September 29, 2007: Message edited by: Acoyani Garrido Sandoval ]
Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.
In some cases, a non-blocking read (or skip) may appear to be blocked when it is merely slow, for example when reading large files over slow networks.
[ emphasis added by me ]
It's possible for available() to return less than the total bytes remaining in the file - it may even return 0 - simply because those bytes are not available right now. For example, the disc drive may be out of position it the moment, and it will take a few milliseconds to position it.
If you want to detect the end of a file, don't use available(). In fact it's hard to imagine anything that available() is really good for - I advise ignoring it in general. Instead, you can look at what the API for read() says:
Returns: the next byte of data, or -1 if the end of the file is reached./quote] If you want to know whether the end of a file has been reached, just check if read() returned a -1. If it didn't, then the file still has data.