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)
Construction Method: ObjectInputStream (inputstream In) creates objectinputstream read from the specified InputStream
deserialization Stream : Restores the Stream object data in the text file or the stream object data in the network to an Object. Stream Data--object (objectinputstream)
Construction Method: ObjectInputStream (inputstream In) creates objectinputstream read from the specified InputStream
Because the operands that are serialized and deserialized are classes, a class is created First:
1 packagezl_copy;2 3 Importjava.io.Serializable;4 5 /*6 //notserializableexception: class is not serialized exception, so the class to be serialized is to implement the Serializable interface7 8 */9 public classAnimalImplements Serializable{Ten one private static final long serialversionuid = 6425500536266476858L; a PrivateString name; -//Private intage ;
* private transient int age; - the - publicAnimal () { - Super(); - //TODO auto-generated Constructor stub + } - + a publicAnimal (String name,intage ) { at Super(); - this. Name =name; - this. Age =age ; - } - - in publicString getName () { - returnname; to } + - the public voidsetName (String Name) { * this. Name =name; $ }Panax Notoginseng - the public intgetage () { + returnage ; a } the + - public voidSetage (intage ) { $ this. Age =age ; $ } - - the @Override - publicString toString () {Wuyi return"Animal [name=" + name + ", age=" + age + "]"; the } - wu -}
Implementation Class:
1 public classObjectstreamdemo {2 3 public Static voidMain (string[] Args)throwsioexception, classnotfoundexception {4 //because the serialized object is a class, you first create a class5 //Serialization Methods6 //Write ();7 //deserialization8 Read (); 9 }Ten one Private Static voidRead ()throwsioexception, ioexception, classnotfoundexception { a // Create a deserialization object - //ObjectInputStream (inputstream In) creates a ObjectInputStream read from the specified InputStream -ObjectInputStream Ois =NewObjectInputStream (NewFileInputStream ("test.txt")); the - //restores the serialized object, regardless of class, anyway it's the parent class is Object -Object o =Ois.readobject (); - + //Freeing Resources - ois.close (); + //output results to the console look a System.out.println (o); at //Animal [name= samoyed, age=2] - - } - - Private Static voidWrite ()throwsIOException { - // Create a serialized object in //ObjectOutputStream (outputstream Out) creates objectoutputstream written to the specified Outputstream. -ObjectOutputStream Oos =NewObjectOutputStream (NewFileOutputStream ("test.txt")); to + //create an object of the class you want to serialize -Animal A =NewAnimal ("samoyed", 2); the * //write to the serialized object, note that the class is written $ Oos.writeobject (a);Panax Notoginseng - //Freeing Resources the oos.close (); + a //java.io.NotSerializableException the //Notserializableexception: class is not serialized exception, so the class to be serialized is to implement the Serializable interface + } - $}
Attention:
Private static final Long Serialversionuid = 6425500536266476858L;:
Why did you write this? If you do not write this, change the variables inside the class, such as:
1 Private int age ; 2 instead: 3 int age;
If this changes, it must be serialized again, or an error will occur:
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.
Let's assume that this tag value is 100.
At the Beginning:
person.class--id=100
Wirte Data: oos.txt--id=100
Read Data: oos.txt--id=100
After you change the age:
person.class--id=200
Wirte Data: oos.txt--id=100
Read Data: oos.txt--id=100
We may also need to use previously written data in Real-world development, but we cannot write again.
so, Let's set this ID value to a fixed value in the Java file so that the ID value will not change when the file is Modified.
The method is: see the class name at the yellow alarm line, click the Mouse.
private transient int age;
There may be a lot of member variables in a class, but some variables do not want to be serialized.
Method: Use the Transient keyword to declare member variables that do not require serialization
Serialization and deserialization of Java 21-13 IO streams