However, if I tried to tell if anything is available like this before I
read the object, it always indicates nothing left.
while (in.available() != 0)
{
}
in.close();
That is, the code inside the while block has never got executed.
Please help.
Haoyu Zhou wrote:
>
> I have a file where there are many objects. I then opened it and read all
> the objects from the file.
> If I do
> while ((ro = in.readObject()) != null)
> {
> //processing the object
> }
> There is no problem until when retrieving the last object(I am still able
> to read it correctly), I got the following exceptions
> java.io.EOFException: Expecting code
...
Is there some particular reason why the EOFException is a problem here,
rather than just being an indication that you have reached the end of
file?
Patricia
while ((ro = in.readObject()) != null)
{
//processing the object
if (in.available() == 0) break;
}
But you are still responsible to catch IOExceptions.
Alex Kizub.
int EOF = -1;
while ((ro = in.readObject()) != EOF) {
The available() method of an InputStream is never a reliable indication
to check for end of file. If it returns non-zero, it means that there is
some data available. But if it returns zero, it just means that a read()
operation is likely to block - not that there is no data left in the
stream. AFAIK the InputStream class does not provide a method that
allows you to check for EOF (except of course catching an EOFException).
BK