• Post Reply Bookmark Topic Watch Topic
  • New Topic

Serializing Vector

 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I successfully serialized my Vector which contains JavaBeans that contain information about films. However, everytime the program is restarted and new JavaBeans are added to the Vector, the Vector is serialized but overwrites the previous serialization. How can I keep adding to the serialized Vector without having it overwritten at each program startup?
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to deserialize the Vector, add your new data, then re-serialize it. You can't just append objects to an ObjectOutputStream:

# Why can't a file that contains multiple appended ObjectOutputStreams be deserialized by one ObjectInputStream?

Using the default implementation of serialization, there must be a one-to-one mapping between ObjectOutputStream construction and ObjectInputStream construction. ObjectOutputStream constructor writes a stream header andObjectInputStream reads this stream header. A workaround is to subclass ObjectOutputStream and override writeStreamHeader(). The overriding writeStreamHeader() should call the super writeStreamHeader method if it is the first write to the file and it should call ObjectOutputStream.reset() if it is appending to a pre-existing ObjectOutputStream within the file.


Object Serialization FAQ
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!