Two Laptop Bag
The moose likes I/O and Streams and the fly likes Serialization problem !!! 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 "Serialization problem !!!" Watch "Serialization problem !!!" New topic

Serialization problem !!!

Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585

I want to serialize a number of objects in a file.

I'm persisting them in a file using the following code:

I made the FileOutputStream's constructor accept true so that it can append more than one serialized object.

And I'm using the following code to deserialize:

Having Persistent time is serialized.

But I'm getting the following exception:

It it deserializing the first object, but other are not.

How can i do it so i can obtain all the serialized objects ?

Thanks in advnace ...

Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
It's quite likely that serialization doesn't support appending to an existing file.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

It doesn't.

Serialization adds a header and I think also a header to the file. Now the thing is, your reading code expects only one single footer. It encounters multiple though, one before each object.

It should still be possible to read it if and only if you have written only a single object at a time, by creating a new ObjectInputStream for every object. Since the backing FileInputStream will progress, it should skip to the next serialization block each time.

There is also another problem in your code. Unlike the InputStream's read methods, there is no return value for identifying the end of the stream. If there are no more object, null is NOT returned but an exception is thrown - I believe.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Serialization problem !!!
It's not a secret anymore!