1 //: Worm.java2 PackageC10;3 4 ImportJava.io.ByteArrayInputStream;5 ImportJava.io.ByteArrayOutputStream;6 ImportJava.io.FileInputStream;7 ImportJava.io.FileOutputStream;8 ImportJava.io.ObjectInputStream;9 ImportJava.io.ObjectOutputStream;Ten Importjava.io.Serializable; One A /** - * @time: Morning 10:35:14 - * @date: May 1, 2017 the * @auther: Skyfffire - * @version: v0.1 - */ - classDataImplementsSerializable { + Private Static Final LongSerialversionuid = --1235946666506705202l; + A Private inti; at -Data (intx) { -i =x; - } - - PublicString toString () { in returninteger.tostring (i); - } to } + - Public classWormImplementsSerializable { the Private Static Final LongSerialversionuid = *7755684981500799364L; $ Panax Notoginseng Private Static intR () { - return(int) (Math.random () * 10); the } + A PrivateData[] D = the{NewData (R ()),NewData (R ()),NewData (R ())}; + PrivateWorm next =NULL; - Private CharC; $ $Worm (intICharx) { -System.out.println ("Worm constructor:" +i); - thec =x; - Wuyi if(---> 0) { theNext =NewWorm (I, (Char) (x + 1)); - } Wu } - About Worm () { $System.out.println ("Default constructor.")); - } - - PublicString toString () { AStringBuffer s =NewStringBuffer ("" + C + "("); + the for(inti = 0; i < d.length; i++) { - S.append (d[i].tostring ()); $ } the theS.append (') '); the the if(Next! =NULL) { - S.append (next.tostring ()); in } the the returns.tostring (); About } the the Public Static voidMain (string[] args) { theWorm W =NewWorm (6, ' a '); + -System.out.println ("w =" +W); the Bayi //Object IO Stream the Try { the //Output -ObjectOutputStream out =NewObjectOutputStream ( - NewFileOutputStream ("Worm.out")); theOut.writeobject ("Worm storage"); the Out.writeobject (w); the out.close (); the - //Input theObjectInputStream in =NewObjectInputStream ( the NewFileInputStream ("Worm.out")); theString s =(String) in.readobject ();94Worm W2 =(Worm) in.readobject (); theSYSTEM.OUT.PRINTLN (S + ", W2 =" +W2); the in.close (); the}Catch(Exception e) {98 e.printstacktrace (); About } - 101 //Byte IO Stream102 Try {103 //Output104Bytearrayoutputstream bout = the NewBytearrayoutputstream ();106ObjectOutputStream out =107 NewObjectOutputStream (bout);108Out.writeobject ("Worm storage");109 Out.writeobject (w); the Out.flush ();111 the //Input113Bytearrayinputstream bin = the NewBytearrayinputstream (Bout.tobytearray ()); theObjectInputStream in = the NewObjectInputStream (bin);117String s =(String) in.readobject ();118Worm W3 =(Worm) in.readobject ();119SYSTEM.OUT.PRINTLN (S + ", W3 =" +W3); - 121 //Close IO Stream122 in.close ();123 out.close ();124}Catch(Exception e) { the e.printstacktrace ();126 }127 } - }129 the ///:~
Output Result:
Worm Constructor:6
Worm Constructor:5
Worm Constructor:4
Worm Constructor:3
Worm Constructor:2
Worm constructor:1
W = a (842) b (627) C (504) d (753) E (709) f (651)
Worm storage, W2 = A (842) b (627) C (504) d (753) E (709) f (651)
Worm storage, W3 = A (842) b (627) C (504) d (753) E (709) f (651)
As you can see, a class that implements the Serializable interface can convert an object to a data stream and restore it.
Serialization (Serializable) can be said to be a super-important thing in Java.
Serializable: For saving and restoring objects--"Thinking in Java" essay 031