Object serialization: Convert an object directly into a byte stream and then write it to a binary file on the hard disk or over the network, and again, we can read the binary into memory and use it to initialize an object that requires a serialized class object. It is necessary to implement the Serializable interface (null interface, which is used in conjunction with instance of the flag), do not need to serialize the property can be modified with transient.
Class:
ObjectOutputStream
ObjectInputStream
Common methods:
ReadObject
WriteObject
It's easy to think of the problem of serializing an object with one pointer pointing to another object, and we should also serialize the object that the pointer is pointing to. In addition, when the object is reorganized after reading, the referenced content should also be modified synchronously.
In C + + notes to do a simple implementation of the program, you can refer to
A basic sample code
class student implements serializable{ public string name; Transient public float score; public student (String name,float score) { this.name = name; this.score = score; }}public Class testio { public static void main (String[] args) { student ss = new student ("Liha", 95.5f); file file = new file ("D:\\object.dat"); try { objectoutputstream objout = new objectoutputstream (New FileOutputStream ( file)); objout.writeobject (ss); objectinputstream Objin = new objectinputstream (New fileinputstream (file)); student sbackstudent = (Student) Objin.readobject (); system.out.println (SbackStudent.name); system.out.println (Sbackstudent.score); objout.close (); objin.close (); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace () ; } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace (); } catch ( Classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace (); } }}
This article is from the "Technical Life" blog, please be sure to keep this source http://6621180.blog.51cto.com/6611180/1922798
Serialization of Java Objects