• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error while reading objects from file

 
sakshi saw
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 9314
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic