1

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....

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.