Nims
Nims

Reputation: 41

Update binary file using ObjectOutputStream

Am writing three objects to a binary file using below code in java.

oout = new ObjectOutputStream(new FileOutputStream(fileName));
oout.writeObject(objClass1);             
oout.writeObject(objClass2);              
oout.writeObject(objClass3);

Then how can i want to modify some data in the objClass1 and update the file? Also append objClass3 to the end of file.

Thanks....

Upvotes: 1

Views: 633

Answers (2)

Stephan
Stephan

Reputation: 8090

You cannot write at the beginning of a file that has contents so you need to read the content , update what you want and write again all the content... this isn't very efficient so i propose 2 solutions :

  • write each object to a separate file
  • use a object store like HashStore , Neodatis, db4o etc
  • you can use a rdms to store the objects (BLOB type in mysql) in a serialized way

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136062

You cannot update just one object, you need to overwrite the whole file. Read all 3 objects from file then write them back with new objClass1 version

Upvotes: 3

Related Questions