Serialization and deserialization learning notes in Java

Source: Internet
Author: User

The person class that needs to be serialized:

Package Cn.itcast_07;import java.io.serializable;/* * notserializableexception: Non-serialized Exception * Class by implementation java.io.Serializable Interface to enable its serialization functionality. Classes that do not implement this interface will not be able to serialize or deserialize any of their states. * This interface is actually not any method, similar to this no method of interface is called labeled interface. * * java.io.InvalidClassException: * Cn.itcast_07.Person; Local class Incompatible: * Stream Classdesc Serialversionuid = -2071565876962058344, * Local class Serialversionuid =- 8345153069362641443 * * Why is there a problem? * The person class implements the serialization interface, so it should also have a tag value. * This tag value is assumed to be 100. * Start: * Person.class--id=100 * Wirte data: oos.txt--id=100 * Read data: oos.txt--id=100 * * Now: * Person.class--id=2 XX * Wirte data: oos.txt--id=100 * Read data: oos.txt--id=100 * We may need to use previously written data in actual development, and cannot be re-written. What do we do? * Recall the reason because their ID values do not match. * Each time you modify the contents of a Java file, the ID value of the class file changes. * While reading a file, it matches the ID value in the class file. So, there's going to be a problem. * But, if I have a way to make this ID value in the Java file is a fixed value, so that when you modify the file, this ID value will also change? Not The key now is how can I know how this ID value is represented? * Don't worry, you don't have to remember, it doesn't matter, just click the mouse. * Don't you see the yellow warning line? * * What we need to know is: * When you see that the class implements the serialization interface, you can automatically generate a serialized ID value if you want to solve the yellow warning line problem. * And when this value is generated, we make any changes to the class and it readsThere is no problem with the previous data. * * NOTE: * I may have a lot of member variables in a class, some of which I don't want to serialize. What should I do? * Use the Transient keyword to declare member variables that do not require serialization */public class person implements Serializable {private static final long Serialversionuid = -2071565876962058344l;private String name;//private int age;private transient int age;//transient keyword indicates that you do not want to be serialized//int age; Public person () {super ();} Public person (String name, int.) {super (); this.name = Name;this.age = age;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} @Overridepublic String toString () {return ' person [name= + name + ', age= ' + Age + '] ";}}

To test the serialization and deserialization classes:

Package Cn.itcast_07;import Java.io.fileinputstream;import Java.io.fileoutputstream;import java.io.IOException; Import Java.io.objectinputstream;import java.io.objectoutputstream;/* * Serialized stream: The object is stored in the same way as a stream in a text file or transmitted over a network. Object-stream Data (objectoutputstream) * Deserialize the stream: Restores the stream object data in the text file or the stream object data in the network to the object. Stream data--object (ObjectInputStream) */public class Objectstreamdemo {public static void main (string[] args) throws Ioexception,cl Assnotfoundexception {//Because we want to serialize the object, we first customize a class//serialization data is actually to write the object to a text file//write (); Read ();} private static void Read () throws IOException, ClassNotFoundException {//Create deserialization object ObjectInputStream ois = new Objectinputs Tream (New FileInputStream ("Oos.txt"));//Restore objects Object obj = Ois.readobject ();//Release Resources ois.close ();// Output Object System.out.println (obj);} private static void Write () throws IOException {//Create serialized Stream object ObjectOutputStream oos = new ObjectOutputStream (New fileoutputs Tream ("Oos.txt"));//create object person p = new person ("Brigitte");//public final void WriteObject (object obj) Oos.writeobject (p); Releasing Resources Oos.close();}} 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Serialization and deserialization learning notes in Java

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.