file - java.io.StreamCorruptedException: Wrong format: ac error android -
i writing serialized objects file , reading after in android. getting " java.io.streamcorruptedexception: wrong format: ac " error while reading second object file. below code writing object file.
public void writetobinary (string filename, object obj, boolean append){ file = new file ("myschedule"); try{ if (!append) { out = new objectoutputstream (getcontext().openfileoutput(filename,context.mode_private)); } else { out = new appendableobjectoutputstream (getcontext().openfileoutput(filename, context.mode_append)); } // toast.maketext(getactivity().getapplicationcontext(), "starting add file", toast.length_short).show(); out.writeobject(obj); out.flush(); toast.maketext(getactivity().getapplicationcontext(), "added file", toast.length_short).show(); }catch (exception e){ e.printstacktrace (); // toast.maketext(getactivity().getapplicationcontext(), "exception file", toast.length_short).show(); }finally{ try{ if (out != null) out.close (); // toast.maketext(getactivity().getapplicationcontext(), "close file", toast.length_short).show(); }catch (exception e){ e.printstacktrace (); } } } private static class appendableobjectoutputstream extends objectoutputstream { public appendableobjectoutputstream(outputstream out) throws ioexception { super(out); } @override protected void writestreamheader() throws ioexception {} }
i calling method using below line of code,every time user clicks on button , writing object file.method called , object saved many times user clicks on button.
if(mainactivity.firsttime) { writetobinary("myschedule", schedulebeans, false); mainactivity.firsttime=false; } else { writetobinary("myschedule", schedulebeans, true); }
here,schedulebeans object of class implements serializable. mainactivity.firsttime public static boolean variable defined in activity , used flag.
now reading file using below code.
objectinputstream ois = null; try { ois = new objectinputstream(getcontext().openfileinput("myschedule")); toast.maketext(getactivity().getapplicationcontext(), "file opened", toast.length_short).show(); while (true) { arraylist.add((schedulebeans) ois.readobject()); toast.maketext(getactivity().getapplicationcontext(), "read file", toast.length_short).show(); } } catch (eofexception e) { toast.maketext(getactivity().getapplicationcontext(), "exception end of file", toast.length_short).show(); } catch (exception e) { e.printstacktrace(); toast.maketext(getactivity().getapplicationcontext(), "exception opening file", toast.length_short).show(); } { try { if (ois != null) { ois.close(); // toast.maketext(getactivity().getapplicationcontext(), "closing file", toast.length_short).show(); } } catch (ioexception e) { e.printstacktrace(); // toast.maketext(getactivity().getapplicationcontext(), "exception in closing file", toast.length_short).show(); } }
i getting error "java.io.streamcorruptedexception: wrong format: ac" while reading second object file in case file has more 1 objects in it.
can please tell should change in code read objects in file successfully?
update-- issue resolved. above code in question edited solved working code.
you're using mode_append in both cases, including case when you're not supposed appending file.
you should use in other case.
Comments
Post a Comment