1. What is serialization and deserialization ?
Serialization (serialization): A process that transforms the state information of an object into a form that can be stored or transmitted. During serialization, an object writes its current state to a temporary or persistent store. Later, the object can be recreated by reading or deserializing the state of the object from the store. The general format is a platform-independent binary stream that can persist this binary stream on disk, or it can transfer this binary stream to another network node over the network.
deserialization (Anti serialization): Refers to restoring this binary stream data to an object.
2. Serializing the stream and deserializing the stream:
serialized stream : ObjectOutputStream
deserialization stream : ObjectInputStream
3. Case Code Demo:
(1) serialization and deserialization are operations on objects.
So before we use the serialized stream (ObjectOutputStream) and the deserialization stream (ObjectInputStream), we first create a class: person, as follows:
1 Packagecn.itcast_07;2 3 Importjava.io.Serializable;4 5 /*6 * notserializableexception: Not serialized exception 7 * 8 * class enables its serialization functionality by implementing the Java.io.Serializable interface . classes that do not implement this interface will not be able to serialize or deserialize any of their states. 9 * This interface is actually not any method, similar to this no method of interface is called labeled interface. Ten * One * We modify the private int age---> int in the Person class to report the following error: A *java.io. invalidclassexception: - * CN.ITCAST_07.PERSON; local class incompatible: - * Stream Classdesc Serialversionuid = -2071565876962058344, the * 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. - * At the beginning: + * Person.class--id=100 A * Write Data: oos.txt--id=100 at * Read data: oos.txt--id=100 - * - * Now: - * Person.class--id=200 ( new ID value is generated after modification ) - * Wirte Data: oos.txt--id=100 - * Read data: oos.txt--id=100 in * We may need to use previously written data in real-world development and cannot be re-written. What do we do? - * Recall the reason because their ID values do not match. to * 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 ? the * Not at all. 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 ?Panax Notoginseng * - * What we want to know is: the * When you see that the class implements the serialization interface, you can automatically generate a serialization ID value to solve the yellow warning line problem . + * And when this value is generated, we make any changes to the class, and it's no problem to read the previous data . A * the * Note: + * I can 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, using default values $ */ $ Public classPersonImplementsSerializable { - Private Static Final LongSerialversionuid = -2071565876962058344l; - the PrivateString name; - Wuyi //private int age; the - Private transient intAge ; Wu - //int age; About $ PublicPerson () { - Super(); - } - A PublicPerson (String name,intAge ) { + Super(); the This. Name =name; - This. Age =Age ; $ } the the PublicString GetName () { the returnname; the } - in Public voidsetName (String name) { the This. Name =name; the } About the Public intGetage () { the returnAge ; the } + - Public voidSetage (intAge ) { the This. Age =Age ;Bayi } the the @Override - PublicString toString () { - return"Person [name=" + name + ", age=" + Age + "]"; the } the}
Test the class as follows:
1 Packagecn.itcast_07;2 3 ImportJava.io.FileInputStream;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 ImportJava.io.ObjectInputStream;7 ImportJava.io.ObjectOutputStream;8 9 /*Ten * 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) One * deserialization 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) A */ - Public classObjectstreamdemo { - Public Static voidMain (string[] args)throwsIOException, the ClassNotFoundException { - //since we're going to serialize the object, we'll start by customizing a class person - //serializing the data is actually writing the object to a text file - //write (); + - read (); + } A at Private Static voidRead ()throwsIOException, ClassNotFoundException { - //creating a Deserialization object -ObjectInputStream Ois =NewObjectInputStream (NewFileInputStream ( -"Oos.txt")); - - //Restore Objects inObject obj =Ois.readobject (); - to //Freeing Resources + ois.close (); - the //Output Object * System.out.println (obj); $ }Panax Notoginseng - Private Static voidWrite ()throwsIOException { the //creating a serialized stream object +ObjectOutputStream Oos =NewObjectOutputStream (NewFileOutputStream ( A"Oos.txt")); the + //Creating Objects -Person p =NewPerson ("Brigitte", 27); $ $ //Public final void writeobject (Object obj) - Oos.writeobject (p); - the //Freeing Resources - oos.close ();Wuyi } the}
Java Fundamentals Hardening IO Stream Note 65: Serializing and deserializing streams