Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A problem till the end of the file.

0 views
Skip to first unread message

Haoyu Zhou

unread,
Dec 12, 2001, 8:35:55 PM12/12/01
to
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
at java.io.ObjectInputStream.peekCode(ObjectInputStream.java:1551)
at
java.io.ObjectInputStream.readObject(ObjectInputStream.java:292)
at
java.io.ObjectInputStream.readObject(ObjectInputStream.java:236)
at chromtest.CHTest.main(CHTest.java:118)

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.


Patricia Shanahan

unread,
Dec 12, 2001, 11:01:34 PM12/12/01
to

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

Alex Kizub

unread,
Dec 13, 2001, 9:20:56 AM12/13/01
to
try this:

while ((ro = in.readObject()) != null)
{
//processing the object

if (in.available() == 0) break;
}

But you are still responsible to catch IOExceptions.

Alex Kizub.

Laine Houghton

unread,
Dec 13, 2001, 2:11:42 PM12/13/01
to
He probably wants this actually:

int EOF = -1;
while ((ro = in.readObject()) != EOF) {

Babu Kalakrishnan

unread,
Dec 20, 2001, 3:24:37 AM12/20/01
to

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

0 new messages