• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can we append/retrieve serialized objects to/from a file?  RSS feed

 
Baps Vakkalagadda
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have few objects in Hashtable. I wrote the Hashtable (Serialized object) to a file. Again I executed the program and added different set of list to Hashtable and the new hashtable is appended to the same file.

Now If i try to retrieve to those serialized objects, I am getting "EOFException (End of File Exception): Expecting code".

It is printing objects one by one correctly but at the end I am getting this error.

Can someone please tell me the solution?
 
Joe Ess
Bartender
Posts: 9443
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can only guess without seeing the code, but are you calling flush() on your ObjectOutputStream to write all your data to disk before calling close()?
 
Baps Vakkalagadda
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe,
Thank you for your reply. Here is the code. I am calling flush() before closing the output stream. I am new to serialization. Please let me know how to read the serialized objects from a file.
 
Joe Ess
Bartender
Posts: 9443
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I can tell that is normal behavior. Your code reads out everything that was written to the file successfully, then it attempts to read more. There is no more, so you get an EOFException. Handle the EOFException seperately and continue as if nothing happened.
I don't think you can append to a file of serialized objects. The first thing an ObjectOutputStream does when it's created is write a serialization header to the file. When you try to append a second ObjectOutputStream to an existing file the second header ends up in the wrong place. Run your code twice and you get a StreamCorruptedException, and I think this is the cause.
If you need to save an unknown number of objects you may be better off placing them in a single collection (i.e. an array list of hashtables), appending to that collection, then overwriting the previous file. Takes care of that nasty EOFException too, since you only read one object out of the file.
 
Baps Vakkalagadda
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it means there is no way that we can append objects to an existing file.
Your answer is very informative. Thank you.
 
Baps Vakkalagadda
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if we have a huge number of objects? Is it still effective to read all the objects each time when we want to append a new object?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Baps V,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

We ask that display names be more than a single letter.

Thanks Pardner! Hope to see you 'round the Ranch!
 
Joe Ess
Bartender
Posts: 9443
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Baps V:
What if we have a huge number of objects? Is it still effective to read all the objects each time when we want to append a new object?

Effective, yes, because it does what you want it to do (i.e. preserve various object's states). Now whether it is efficient to read in a huge array of objects just so you can add one more, and exactly at what number the array becomes unmanagable, you need to consider your program's requirements in order to answer that question. Depending on your circumstances you may want to look at writing out the objects' states to file rather than using serialization so you can append to the data file, or using a database.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!