Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization problem !!!

 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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 ...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's quite likely that serialization doesn't support appending to an existing file.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 - java.io.EOFException I believe.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic