49-Object serialization

Object serialization

  Almost as long as it is Java development, there must be a concept of serialization. As the concept of serialization has gradually developed, there are gradually more serialization standards.

Basic concepts of object serialization

  The so-called object serialization refers to the processing of objects stored in memory in the form of a binary data stream, which can realize object storage or network transmission.   However, not all objects can be serialized. There is a mandatory requirement in Java: If you want to serialize an object, the class where the object is located must implement the java.io.Serializable parent interface as a serialization mark . This interface does not have any methods, because it describes the capabilities of a class. Define a class that can be serialized
y5cIT1.png

import java.io.Serializable;

@SuppressWarnings("serial")		//压制警告
class Person implements Serializable{
    
    	//Person可以被序列化	
	private String name;
	private int age;
	public Person(String name, int age) {
    
    
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
    
    
		return "Person [name=" + name + ", age=" + age + "]";
	}
}

  At this time, every object produced by the Person class can realize binary data transmission and belong to a program class that can be serialized.

Serialization and deserialization processing

  After having serialized support classes, if you want to implement serialization and deserialization operations, you can use the following two classes to complete:

Class name Serialization: ObjectOutputStream Deserialization: ObjectInputStream
Class definition public class ObjectOutputStream extends OutputStream implements ObjectOutput,ObjectStreamConstants public class ObjectInputStream extends implements ObjectInput,ObjectStreamConstants
Construction method public ObjectOutputStream(OutputStream out) throws IOException public ObjectInputStream(InputStream in) throws IOException
method public final void writeObject(Object obj) throws IOException public final Object readObject() throws IOException,ClassNotFoundException

Realize serialization and deserialization

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

@SuppressWarnings("serial")		//压制警告
class Person implements Serializable{
    
    	//Person可以被序列化	
	private String name;
	private int age;
	public Person(String name, int age) {
    
    
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
    
    
		return "Person [name=" + name + ", age=" + age + "]";
	}
	
}

public class Basic_study {
    
    
	public static final File SAVE_FILE = new File("C:\\Project\\Java_study\\src\\文件\\test.person");
	public static void main(String[] args) throws Exception {
    
    
		//saveObject(new Person("lyz",18));
		System.out.println(loadObject());
	}
	public static void saveObject(Object obj) throws Exception{
    
    
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(SAVE_FILE));
		oos.writeObject(obj);		//序列化
		oos.close();
	}
	public static Object loadObject() throws Exception{
    
    
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(SAVE_FILE));
		Object obj = ois.readObject();		//反序列化
		ois.close();
		return obj;
	}
}

  Object serialization and deserialization in java must use the internally provided object operation flow, because it involves the format of binary data, so it cannot be customized. In addition, if you want to realize the instantiation of a group of objects, you can use an object array to complete.
  In many actual project development, developers rarely see the direct operation of ObjectOutputStream and ObjectInputStream classes, because there will be some containers to help developers automatically implement them.

transient keyword

  By default, when the object serialization is performed, all the contents of all attributes in the class will be serialized, but in many cases there are some attributes that do not need to be serialized. After this meeting, you can define the attributes. Use the transient keyword to complete.
private transient String name;
  During serialization, the content of the name attribute isWill not be savedNext, in other words, the read data name will be the default value of its corresponding data type.
  If it is assumed that some properties in the class that need to be calculated and saved often do not need to be serialized, use transient at this time. In actual development, most of the classes that need to be serialized are often simple java classes.

Guess you like

Origin blog.csdn.net/MARVEL_3000/article/details/114440939