Java serialization and deserialization learning notes
Person class to be serialized:
Package cn. itcast_07; import java. io. Serializable;/** NotSerializableException: Non-serialization exception ** class to enable its serialization function by implementing the java. io. Serializable interface. Classes that do not implement this interface cannot be serialized or deserialized in any State. * This interface does not have any methods. Similar to this interface without methods, it is called a tag interface. ** Java. io. InvalidClassException: * cn. itcast_07.Person; local class incompatible: * stream classdesc serialVersionUID =-2071565876962058344, * local class serialVersionUID =-8345153069362641443 ** why? * If the Person class implements the serialization interface, it should also have a tag value. * Assume that the tag value is 100. * Start Time: * Person. class -- id = 100 * wirte data: oos.txt -- id = 100 * read data: oos.txt -- id = 100 ** now: * Person. class -- id = 200 * wirte data: oos.txt -- id = 100 * read data: oos.txt -- id = 100 * in actual development, we may also need to use previously written data, data cannot be written again. What should we do? * The reason is that their id values do not match. * Each time you modify the content of a java file, the id value of the class file changes. * When reading a file, it will match the id value in the class file. So there will be problems. * But if I have a way to make this id value a fixed value in the java file, will this id value change when you modify the file? * No. Now the key 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. * Didn't you see a yellow warning line? ** What we need to know is: * When the class implements the serialization interface, we can automatically generate a serialized id value to solve the yellow warning line problem. * After this value is generated, we make any changes to the class. It is no problem to read the previous data. ** Note: * a class may have many member variables, and some do not want to be serialized. What should I do? * Use the transient keyword to declare the member variables that do not need to be serialized */public class Person implements Serializable {private static final long serialVersionUID =-2071565876962058344L; private String name; // private int age; private transient int age; // The transient keyword indicates that the object is not serialized. // int age; public Person () {super ();} public Person (String name, int age) {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 age) {this. age = age ;}@ Overridepublic String toString () {return Person [name = + name +, age = + age +] ;}}
Classes for testing serialization and deserialization:
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: stores objects in text files or transmits them over the network in the same way as the stream. Object-stream data (ObjectOutputStream) * deserialization stream: Restores Stream object data in text files or stream object data in the network to an object. Stream Data -- Object (ObjectInputStream) */public class ObjectStreamDemo {public static void main (String [] args) throws IOException, ClassNotFoundException {// because we want to serialize the object, so first we define a class/serialized data to write the object to a text file // write (); read ();} private static void read () throws IOException, classNotFoundException {// create the deserialization Object ObjectInputStream ois = new ObjectInputStream (new FileInputStream(oos.txt); // restore the Object obj = ois. readObject (); // release the resource ois. close (); // output object System. out. println (obj);} private static void write () throws IOException {// create a serialized Stream object ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream(oos.txt )); // create Object Person p = new Person (Lin Qingxia, 27); // public final void writeObject (Object obj) oos. writeObject (p); // release the resource oos. close ();}}