Finishing knowledge
Knowledge maps
Object
For details, please click (Object)
Common API
For details, please click (String class)
other common String Math and System
Map
For details, please click (Map Tree and use)
File
Serialization
concept
Serialization refers to the status information of the object may be converted for storage or transmission process. During serialization, the object status is written to a temporary or permanent storage area, can be read or deserialize status storage area, re-create the object.
Precautions
- If a class is required sequence, it is necessary to comply with Serializable interface, to obtain a sequence of numbers.
public class Person implements Serializable {
/**
* 序列化编号,这个编号可以自己设也可以系统给定
*/
private static final long serialVersionUID = 1L;
- transient member variables can not be modified sequence of (transient).
method
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("D:/aaa/person.txt"));
// 序列化对象,并且写入到文件中 id = 1 name = "小明" age = 16
objectOutputStream.writeObject(new Person(1, "小明", 16));
After serialize the contents of
deserialization
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("E:/创建文件/person.txt"));
Person person = (Person) objectInputStream.readObject();
//输出为 Person [id=1, name=小明, age=16]
System.out.println(person);
If I add member variables to the Person of the transient, the corresponding member variables when deserialized, its value is the initial value.