File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes error while reading objects from file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "error while reading objects from file" Watch "error while reading objects from file" New topic

error while reading objects from file

sakshi saw

Joined: Jun 06, 2009
Posts: 6
Below is a short code to explain the problem i am facing
i have been working on the same for past one week i am facing exceptions while reading a file containing more than one objects

would be very grateful for early reply!
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

If you are getting an exception, it is a good idea to share it with us. How are we to know what your code is doing?
I anticipate you are getting an OptionalDataException. If you look at the Java Documentation for ObjectInputStream:
Any attempt to read object data which exceeds the boundaries of the custom data written by the corresponding writeObject method will cause an OptionalDataException to be thrown with an eof field value of true.

Many people find that relying on an exception to signal an expected condition like EOF is counter-intuitive. The most common work-around is to either write the number of objects to the file then read only that number of times, or to put the objects in a collection and do a single read to load the collection and all the serialized objects (I prefer the latter).

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: error while reading objects from file
It's not a secret anymore!