> It is specified for readObject that it will throw an exception. I
> only want to know how to create a loop reading those Objects without
> reading more then there is in file. I just don't know what statement
> should i put in the loop. I really don't want that exception to be
> thrown.
What is your aversion to the exception, when it solves your problem?
try {
while ((o = ois.readObject())) {
/* more stuff */
}
}
catch (EOFException e) {
System.out.println("Finished reading objects");
}
finally {
ois.close();
}
Alternatives include writing a special sentinal object last in the
stream or using some other mechanism to communicate the number of
objects to expect, but to me these are superfluous.
You still need to be able to handle that exception (i.e. you may never
reach the sentinal object or the expected count), and when it occurs
you have reached EOF whether you like it or not.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Krystian - 27 Jan 2006 10:49 GMT
> You still need to be able to handle that exception (i.e. you may never
> reach the sentinal object or the expected count), and when it occurs
> you have reached EOF whether you like it or not.
And this is why i am seeking for a different way to solve this then by
exeption as it can happen when ie. file is corrupted and then it would
only tell me that everything is done.
I thought about adding something at the end of the file but it was
going to be my final way of solving this.
As there is no other... i guess this must suffice.
Thank you for your help ;)
Best regards
Nigel Wade - 30 Jan 2006 11:31 GMT
>> You still need to be able to handle that exception (i.e. you may never
>> reach the sentinal object or the expected count), and when it occurs
[quoted text clipped - 3 lines]
> exeption as it can happen when ie. file is corrupted and then it would
> only tell me that everything is done.
No, it would not tell you that everything was done. You will get a different
exception if the file is corrupted. You need to handle them differently because
they are telling you different things. If you receive an EOFException it means
that you have successfully read all the objects from the stream, any other
IOException indicates that you have not successfully read the all the objects.

Signature
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555