Java.io.Serializable Serialization Problems

Source: Internet
Author: User

Java.io.Serializable Serialization Problems

Person.java
 PackageA.B.C; Public classPersonImplementsjava.io.serializable{//private static final long serialversionuid = 2481632967198330813L;String field1;    String Field2;    String field3;  PublicString getField1 () {returnfield1; }     Public voidsetField1 (String field1) { This. field1 =field1; }     PublicString getField2 () {returnField2; }     Public voidsetField2 (String field2) { This. Field2 =Field2; }     PublicString getField3 () {returnfield3; }     Public voidsetField3 (String field3) { This. field3 =field3; }    }
View Code

Student.java Student inherits from person, but in order to be clear, the person is exactly the same and covers it again.
 PackageA.B.C; Public classStudentextendsPersonImplementsjava.io.Serializable {//private static final long serialversionuid = 2481632967198330813L;String field1;    String Field2;    String field3;  PublicString getField1 () {returnfield1; }     Public voidsetField1 (String field1) { This. field1 =field1; }     PublicString getField2 () {returnField2; }     Public voidsetField2 (String field2) { This. Field2 =Field2; }     PublicString getField3 () {returnfield3; }     Public voidsetField3 (String field3) { This. field3 =field3; }}
View Code

Serializablewriter.java
 PackageA.B.C;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream; Public classSerializablewriter { Public Static voidMain (string[] args)throwsexception{write (); }         Public Static voidWrite ()throwsexception{Student Stu=NewStudent (); Stu.setfield1 ("Aaa111"); Stu.setfield2 ("Bbb222"); ObjectOutputStream ObjectOutputStream=NewObjectOutputStream (NewFileOutputStream ("C:/users/king/desktop/jx.txt"));            Objectoutputstream.writeobject (Stu);            Objectoutputstream.flush ();    Objectoutputstream.close (); }}

Serializablereader.java
 PackageA.B.C;ImportJava.io.FileInputStream;ImportJava.io.ObjectInputStream; Public classSerializablereader { Public Static voidMain (string[] args)throwsexception{read (); }         Public Static voidRead ()throwsexception{ObjectInputStream ObjectInputStream=NewObjectInputStream (NewFileInputStream ("C:/users/king/desktop/jx.txt")); Object obj=Objectinputstream.readobject (); Person Person=(person) obj;            System.out.println (Person.getfield1 ());            System.out.println (Person.getfield2 ());    Objectinputstream.close (); }}
Print results
aaa111bbb222

Summarize
    • As long as the version number is consistent, and student even delete the properties and methods of Field3, the serialized student can still be deserialized into student.
    • As long as the version number is consistent and student inherits from the parent class person, the serialized student can be deserialized into person.

Java.io.Serializable Serialization Problems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.