Reading from file Java null Object -
i write , read object file. below enclose code sample show idea (it sample, object more complex, problem same).
my problem is:
if don't make testobject field static, testobject.points becomes null on start of readobject method. don't understand why. give me explanation?
i write 1 object, , 2 objects, , after read them (multiple object writing/reading - want create log file android).
could me?
// ... import
public class testobject implements serializable{ transient public arraylist<point[]> points; public testobject() { points = new arraylist<point[]>(); point[] p1 = new point[1]; p1[0] = new point(1,1); point[] p2 = new point[2]; p2[0] = new point(2,2); p2[1] = new point(2,2); points.add(p1); points.add(p2); } private void writeobject(objectoutputstream stream) throws ioexception { stream.defaultwriteobject(); stream.writeint(points.size()); point[] pointsarray = null; (int = 0; < points.size(); i++) { pointsarray = ((point[])points.get(i)); stream.writeint(pointsarray.length); (int j = 0; j < pointsarray.length; j++) { stream.writeint(pointsarray[j].x); stream.writeint(pointsarray[j].y); } } } private void readobject(java.io.objectinputstream stream) throws ioexception, classnotfoundexception { stream.defaultreadobject(); points = new arraylist<point[]>(); int pointssize = stream.readint(); (int = 0; < pointssize; i++) { int arraysize = stream.readint(); point[] pointsarray = new point[arraysize]; (int j = 0; j < arraysize; j++) { pointsarray[j] = new point(stream.readint(), stream.readint()); } points.add(pointsarray); } } public void writelog() { file file = new file ("c:\\!\\", "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 testobject readlog() { testobject testobject = new testobject(); testobject.points = new arraylist<point[]>(); try{ file file = new file ("c:\\!\\", "data.log"); fileinputstream fis = new fileinputstream(file); objectinputstream reader = new objectinputstream(fis); testobject = (testobject) reader.readobject(); reader.close(); } catch (exception e) { //todo auto-generated catch block e.printstacktrace(); } return testobject; } }
and mine main class
// ... import
public class main { public static void main(string[] args) throws ioexception { testobject testobject = new testobject(); testobject.writelog(); testobject.readlog(); try { thread.sleep(100); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
again, example. real application android.
import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import java.io.serializable; import java.util.arraylist; import java.util.linkedlist; import android.graphics.point; import android.os.environment; public class loginfo implements serializable{ /** * */ private static final long serialversionuid = 2281758309050283667l; transient public arraylist<point[][]> strokes; transient public linkedlist<byte[]> codes; public loginfo() { strokes = new arraylist<point[][]>(); codes = new linkedlist<byte[]>(); } 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)); } } private void readobject(java.io.objectinputstream stream) throws ioexception, classnotfoundexception { strokes = new arraylist<point[][]>(); 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(); codes = new linkedlist<byte[]>(); (int = 0; < codessize; i++) { byte[] buffer = new byte[3]; stream.read(buffer, 0, 3); codes.add(buffer); } } 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 loginfo readlog() { try{ file file = new file (environment.getexternalstoragedirectory().getabsolutepath(), "data.log"); fileinputstream fis = new fileinputstream(file); objectinputstream reader = new objectinputstream(fis); reader.readobject(); reader.close(); } catch (exception e) { //todo auto-generated catch block e.printstacktrace(); } return this; } }
i call writelog , readlog different class, have object instance.
// write file loginfo.writelog(); // clear log variable (not file) delallloginfo(); // read file loginfo newloginfo = loginfo.readlog();
- if don't make testobject field static, testobject.points becomes null on start of readobject method. don't understand why. give me explanation?
transient fields deserialized null. see object serialization specification. static fields aren't deserialized @ all, remain @ current value.
- i write 1 object, , 2 objects, , after read them (multiple object writing/reading - want create log file android).
if have same file open @ times non-problem. if don't, need open file in append mode second , subsequent times, , use derived class of objectoutputstream
doesn't write header second , subsequent times.
Comments
Post a Comment