• Post Reply Bookmark Topic Watch Topic
  • New Topic

Appending an object to a file  RSS feed

 
Mike London
Ranch Hand
Posts: 1476
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you **append** an object into a file?
Since there isn't an "append" method in the ObjectOutputStream class, for example, do you first have to read in all the objects from the file into say, a Vector, add the new one, and then write them all back out?
Or, better yet, is needing to append a record to a file a good case for a RandomAccess file?
I may be missing something simple here, but I appreciate, in advance, anyone's reply!
Thanks!!!
-- Mike
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since there isn't an "append" method in the ObjectOutputStream class, for example, do you first have to read in all the objects from the file into say, a Vector, add the new one, and then write them all back out?
Yes, that's one good way. Except I'd say ArrayList rather than Vector. Or you might put separate objects in separate files entirely. That's relatively simple to understand. But beware that if the objects you're serializing contain references to other objects, those other objects will be serialized too (if they haven't already been serialized by the current OOS), even when you "just" serialize one object. If you try to split your objects into separate files, but the objects have links to each other (or to other shared objects) you may end up with a lot of confusion, with multiple clones of each object. If you've got a complex interlinked structure to your objects, you probably want to serialize all objects using the same single ObjectOutputStream, in the same file. (Serializing an ArrayList of objects is a simple example of this.)
 
Mike London
Ranch Hand
Posts: 1476
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for your reply!!!
-- Mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!