Serialization of objects and the inverse sequence

Source: Internet
Author: User

One: Demand analysis

In the actual B/s structure, the object serialization and deserialization is very important, especially in the case of large request volume, the server pressure is very large,

Will serialize part of the session and then save it to the hard drive.

Two: Define a Dto Object

The defined entity class must implement the Serializable interface before it can be serialized.

1 /**2  * 3  */4  Packagecom.hlcui.dto;5 6 Importjava.io.Serializable;7 8 /**9  * @authorAdministrator student entity classTen  */ One  Public classStudentImplementsserializable{ A      -     /** -      *  the      */ -     Private Static Final LongSerialversionuid = 1L; -     PrivateInteger ID; -     PrivateString name; +     PrivateInteger age; -     PrivateDouble score; +  A      PublicStudent () { at  -     } -  -      PublicStudent (integer ID, String name, Integer age, Double score) { -         Super(); -          This. ID =ID; in          This. Name =name; -          This. Age =Age ; to          This. score =score; +     } -  the      PublicInteger getId () { *         returnID; $     }Panax Notoginseng  -      Public voidsetId (Integer id) { the          This. ID =ID; +     } A  the      PublicString GetName () { +         returnname; -     } $  $      Public voidsetName (String name) { -          This. Name =name; -     } the  -      PublicInteger getage () {Wuyi         returnAge ; the     } -  Wu      Public voidsetage (Integer age) { -          This. Age =Age ; About     } $  -      PublicDouble Getscore () { -         returnscore; -     } A  +      Public voidSetScore (Double score) { the          This. score =score; -     } $  the @Override the      PublicString toString () { the         return"Student [age=" + Age + ", id=" + ID + ", name=" +name the+ ", score=" + score + "]"; -     } in  the}

Three: Encapsulation serialization and deserialization method

1 //Serializing Objects2      Public Static voidSerlizableobject (Student Stu)throwsIOException {3FileOutputStream fos =NewFileOutputStream (NewFile ("F:/stu.txt"));4ObjectOutputStream Ois =NewObjectOutputStream (FOS);5 Ois.writeobject (stu);6 Ois.flush ();7 ois.close ();8     }9 Ten     //deserializing Objects One      Public StaticStudent Tranferserobject ()throwsException { AFileInputStream FIS =NewFileInputStream (NewFile ("F:/stu.txt")); -@SuppressWarnings ("Resource") -ObjectInputStream Ois =NewObjectInputStream (FIS); the         return(Student) ois.readobject (); -}

Four: Testing

1 //Test2      Public Static voidMain (string[] args)throwsException {3Student stu =NewStudent ();4Stu.setid (1);5Stu.setname ("Tom");6Stu.setage (26);7Stu.setscore (9000.0);8Serlizableobject (Stu);//Serialization of9Student Student =Tranferserobject ();Ten System.out.println (student); One}

V: Show Results

Six: Pay attention.

There is a sequence version number in the entity class student class, and if you do not write it, you may not be able to deserialize some of the attributes in the entity.

1: Remove the version number and then perform serialization.

2: In the Student class, add a property, gender gender

3: Perform deserialization, the following error will be reported.

Exception in thread "main" java.io.InvalidClassException:com.hlcui.dto.Student; Local class Incompatible:stream Classdesc serialversionuid = 8188927919700027025, local class Serialversionuid =-5275569 862405728323

At Java.io.ObjectStreamClass.initNonProxy (objectstreamclass.java:562)
At Java.io.ObjectInputStream.readNonProxyDesc (objectinputstream.java:1583)
At Java.io.ObjectInputStream.readClassDesc (objectinputstream.java:1496)
At Java.io.ObjectInputStream.readOrdinaryObject (objectinputstream.java:1732)
At Java.io.ObjectInputStream.readObject0 (objectinputstream.java:1329)
At Java.io.ObjectInputStream.readObject (objectinputstream.java:351)
At Com.hlcui.io.ObjectSer.tranferSerObject (objectser.java:35)
At Com.hlcui.io.ObjectSer.main (objectser.java:46)

cannot be deserialized.

4: If we add a version number, after serialization, adding properties, after deserialization

All of the above codes have been verified!

Serialization of objects and the inverse sequence

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.