The data in the object, in the form of a stream, is written to a file that is saved
Processes are called write-out objects, and the serialization of objects
ObjectOutputStream writing objects to a file for serialization
In the file, in the form of a stream, the object is read out,
Read object, deserialization of object
ObjectInputStream read the file object to enable deserialization
Example:
Simply write a class:
Packagedemo;Importjava.io.Serializable; Public classPersonImplementsSerializable {PrivateString name; Private intAge ; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } @Override PublicString toString () {return"Person [name=" + name + ", age=" + Age + "]"; } PublicPerson (String name,intAge ) { Super(); This. Name =name; This. Age =Age ; } PublicPerson () {}}
Test:
Packagedemo;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream; Public classObjectstreamdemo { Public Static voidMain (string[] args)throwsIOException, ClassNotFoundException {function1 ();//WriteFunction2 ();//Read } Public Static voidFunction1 ()throwsIOException {fileoutputstream fos1=NewFileOutputStream ("D:\\person.txt"); ObjectOutputStream OOS1=NewObjectOutputStream (FOS1); Person P1=NewPerson ("Zhangsan", 18); Oos1.writeobject (p1); Oos1.close (); } Public Static voidFunction2 ()throwsIOException, classnotfoundexception {fileinputstream fis1=NewFileInputStream ("D:\\person.txt"); ObjectInputStream Ois1=NewObjectInputStream (FIS1); Object Object=Ois1.readobject (); System.out.println (object); Ois1.close (); }}
Precautions:
1. Static cannot be serialized, if you add the age of the person class to static and then serialize the operation, it is found that no matter what, there is always a age=0
Cause: The object is serialized, but Static does not belong to the object, belongs to the class, the object does not contain static, so static can not serialize, age=0 because the default is 0
Transient keyword: block member variable serialization
Sometimes you don't want the member variable to be serialized, but you can't define it as static, and then add the transient keyword
Again, the person class above is the implementation class for the Serializable interface:
There is no method in this interface, called a labeled interface, that acts as a marker, and if the person class is tagged, it can be serialized
Serial number conflict problem:
The principle of popular interpretation:
Defines a person class, defines two private member variables, compiles and generates a new file Person.class file
The compiler calculates a sequence number for a class file based on its definition, and writes the class file to a file
When reading, the serial number is compared and the serial number is the same, the reverse sequence succeeds
If the source of the person class is suddenly modified, then the newly compiled class file and the previous serial number are different,
However, the file is saved in the previous serial number, when the comparison of serial numbers, inconsistent, so will throw an exception
Ways to resolve serial number conflicts:
To customize the serial number in your custom class:
Join this line, the former part can not be changed, the back of the number may be arbitrarily changed
// fixed notation Private Static Final long serialversionuid = 42L;
Java Learning Note 42 (serialized stream)