java - Appending Objects to a serialization file -


suppose have appendobjectoutputstream class (which objectoutputstream!) overrides writestreamheader() this:

@override public void writestreamheader() throws ioexception {     reset(); } 

now also, let's plan on saving multiple objects file; 1 object each time program runs. you, on first run, use appendobjectoutputstream()?

you have write stream header first time regular objectoutputstream otherwise java.io.streamcorruptedexception on opening file objectinputstream.

public class test1 implements serializable {      public static void main(string[] args) throws exception {         objectoutputstream os1 = new objectoutputstream(new fileoutputstream("test"));         os1.writeobject(new test1());         os1.close();          objectoutputstream os2 = new objectoutputstream(new fileoutputstream("test", true)) {             protected void writestreamheader() throws ioexception {                 reset();             }         };          os2.writeobject(new test1());         os2.close();          objectinputstream = new objectinputstream(new fileinputstream("test"));         system.out.println(is.readobject());         system.out.println(is.readobject()); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -