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
Post a Comment