1 PackageCom.io;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 ImportJava.io.ObjectInputStream;8 ImportJava.io.ObjectOutputStream;9 Importjava.io.Serializable;Ten One Importorg.junit.Test; A - /** - * the * 1, object serialization mechanism: Convert in-memory Java objects into platform-independent binary streams, - * Persistent storage on disk or transmission over network to another node - * 2, deserialization: After the other program obtains the binary stream, it can be restored to the original object - * + * - */ + Public classObject Stream { A @Test at Public voidobjoutputstreamtest () { -person P1 =NewPerson ("xiaoming", 22); -person P2 =NewPerson ("Little Red", 23); - -ObjectOutputStream Oos =NULL; - Try { inOos =NewObjectOutputStream (NewFileOutputStream (NewFile ("Obj.txt"))); - Oos.writeobject (p1); to oos.writeobject (p2); + Oos.flush (); -}Catch(IOException e) { the //TODO auto-generated Catch block * e.printstacktrace (); $}finally{Panax Notoginseng Try { - oos.close (); the}Catch(IOException e) { + e.printstacktrace (); A } the } + } - $ @Test $ Public voidReadObject () { -ObjectInputStream Ois =NULL; - Try { theOIS =NewObjectInputStream (NewFileInputStream (NewFile ("Obj.txt"))); - Wuyiperson P1 =(person) ois.readobject (); theObject P2 =(Object) ois.readobject (); - System.out.println (p1); Wu System.out.println (p2); -}Catch(IOException e) { About //TODO auto-generated Catch block $ e.printstacktrace (); -}Catch(ClassNotFoundException e) { - //TODO auto-generated Catch block - e.printstacktrace (); A}finally{ + Try { the ois.close (); -}Catch(IOException e) { $ //TODO auto-generated Catch block the e.printstacktrace (); the } the } the } - } in the /** the * About * @authorAdministrator the * Class requirements to implement serialization: the * (1) class is serializable: Implements serializable interface the * (2) The properties of the class are serializable, and the attributes inherit the serializable interface + * (3) Properties modified with the static and transient keywords cannot be serialized - * (4) provide a version number Serialversionuid the */Bayi the the /* - * All classes that implement the Serializable interface have a static variable that represents the serialized version identifier: - private static final long serialversionuid; the serialversionuid used to indicate compatibility between different versions of a class the if the class does not show the definition of this static variable, its value is automatically generated by the Java Runtime Environment based on the inner details of the class. If the source code of the class is modified, the serialversionuid may change. Therefore, it is recommended to display the statement the Display the purpose of defining Serialversionuid the you want the different versions of the class to be serializable compatible, so make sure that the different versions of the class have the same Serialversionuid - You do not want different versions of the class to be serializable compatible, so make sure that different versions of the class have different Serialversionuid the the */ the classPersonImplementsserializable{94 /** the * the */ the Private Static Final LongSerialversionuid = -8305586168290840729l;98 About PrivateString name; - 101 Private intAge ;102 103 PublicPerson (String Name,integer age) {104 This. Name =name; the This. Age =Age ;106 }107 108 /*(Non-javadoc)109 * @see java.lang.object#tostring () the */111 @Override the PublicString toString () {113 return"Person [name=" + name + ", age=" + Age + "]"; the } the the 117}
6-Object Flow (serialization of objects)