java - Write and read multiple objects to file -
i designing handwriting application android.
i write information (class loginfo) log file, every time user presses enter button.
after that, read stored information.
this part of class custom write method:
public class loginfo implements serializable { private static final long serialversionuid = -5777674941129067422l; public static list<point[][]> strokes; public static list<byte[]> codes; // write , read methods shown private void writeobject(objectoutputstream stream) throws ioexception { stream.defaultwriteobject(); stream.writeint(strokes.size()); point[][] pointsarray = null; (int = 0; < strokes.size(); i++) { pointsarray = ((point[][])strokes.get(i)); stream.writeint(pointsarray.length); (int j = 0; j < pointsarray.length; j++) { stream.writeint(pointsarray[j].length); (int k = 0; k < pointsarray[j].length; k++) { stream.writeint(pointsarray[j][k].x); stream.writeint(pointsarray[j][k].y); //stream.writeobject(elementdata[i]); } } } int size = codes.size(); stream.writeint(size); (int = 0; < size; i++) { stream.write(codes.get(i)); } } this read method:
private void readobject(java.io.objectinputstream stream) { stream.defaultreadobject(); int strokessize = stream.readint(); (int = 0; < strokessize; i++) { int arrayxsize = stream.readint(); point[][] points = new point[arrayxsize][]; (int j = 0; j < arrayxsize; j++) { int arrayysize = stream.readint(); points[j] = new point[arrayysize]; (int k = 0; k < arrayysize; k++) points[j][k] = new point(stream.readint(), stream.readint()); } strokes.add(points); } int codessize = stream.readint(); (int = 0; < codessize; i++) { byte[] buffer = new byte[3]; stream.read(buffer, 0, 3); codes.add(buffer); } } it works when save 1 object in file. when try save more, reading not working (it throws streamcorruptedexception). it reads 1 object in while loop!
in main class, use 2 simple methods:
// write file loginfo.writelog(); // read file arraylist<loginfo> loginfoarraylist = loginfo.readlog(); defined as:
public void writelog() { file file = new file (environment.getexternalstoragedirectory().getabsolutepath(), "data.log"); fileoutputstream fos; try { fos = new fileoutputstream(file, true); //fos = openfileoutput(environment.getexternalstoragedirectory().getabsolutepath() + "/data.log", context.mode_append); objectoutputstream os = new objectoutputstream(fos); os.writeobject(this); os.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } public arraylist<loginfo> readlog() { arraylist<loginfo> loginfoarray = new arraylist<loginfo>(); try{ file file = new file (environment.getexternalstoragedirectory().getabsolutepath(), "data.log"); fileinputstream fis = new fileinputstream(file); objectinputstream reader = new objectinputstream(fis); loginfo temploginfo = new loginfo(); while((temploginfo = (loginfo)reader.readobject()) != null) loginfoarray.add(temploginfo); reader.close(); } catch (exception e) { //todo auto-generated catch block e.printstacktrace(); } return loginfoarray; } requested update:
//we use class not write header in file exist class myobjectoutputstream extends objectoutputstream { public myobjectoutputstream(outputstream os) throws ioexception { super(os); } @override protected void writestreamheader() {} }
you can't append existing file created
objectoutputstream, @ least not without effort. there trick somewhere extendingobjectoutputstream, overridingwritestreamheader()method not write stream header second time, i'm not in favour of it. should rewrite whole file, maybe list.you don't need code. make
strokes,codesnon-static , non-transient, , rid ofreadobject(),writeobject()methods altogether.
Comments
Post a Comment