• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serialization problem !!!

 
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's quite likely that serialization doesn't support appending to an existing file.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Then YOU must do the pig's work! Read this tiny ad. READ IT!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic