(Miscellaneous) Java serialization Serializable and deserialization

1. What is java serialization?

The object into a sequence of bytes is called serialization
sequence of bytes into object deserialization process is known.

2. Why do you need serialization?

There are two main application scenarios for serialization:
  1. Used to save objects from memory to disk.
  2. Used to transfer objects on the network

    Here is an example to explain: For
    example, the Session object storage problem often encountered in Web development, assuming that 10W concurrent requests arrive, it is too much to generate 10W Session objects in a short period of time in memory, what should I do? First save these 10W Session objects to disk permanently, then in a short time, according to the incoming request, restore the Session objects in the memory one by one. At this time, the best thing to save on the disk is the Java serialized object, the smallest It occupies disk memory.
    Besides remote network communication, the data on the network is in the form of a binary sequence. Generally, the requester will send the byte sequence of the java object, and the receiver will restore the java object according to the byte sequence. What are the benefits? The byte sequence memory of the object is much smaller than the object memory, which can shorten the request-response time.
Precautions
3. If the object contains object attributes, the class of the object must also implement the Serializable interface, otherwise the object cannot be serialized. 4. The serialization operation ignores the state of static variables. For example, static attributes in objects cannot be serialized. Static attributes are class-level, not object-level.

3. Serialization process and code examples

First introduce the API in the JDK library to be used:

1.java.io.OutputStream object output stream, through this class (or its subclass FileOutputStream, ByteOutputStream) to create an object that can be serialized, and then pass the object to the java.io.ObjectOutputStream object, the writeObject ( The method accepts an OutputStream object to complete the serialization process.
2. The java.io.InputStream object input stream, accepts an object input stream, and then calls the java.io.ObjectInputStream.readObject() method to complete the serialization. (See the code chestnut for details)


java serialization object code
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class SerializeInstance implements Serializable {

	/**
	 * the default serialVersion UID;
	 */
	private static final long serialVersionUID = 1L;
	public int num = 1209;
	public static void main(String []args)   {
		try {
			//1.the process of the serializing 
			FileOutputStream fileOutputStream = new FileOutputStream("D:\\JavaCodes\\Examps\\JasonWord.txt");
			ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream);
			 SerializeInstance instance = new SerializeInstance();
			 outputStream.writeObject(instance);
			 outputStream.flush();
			 outputStream.close();
			 
		}catch(IOException e) {
			e.printStackTrace();
		}
		
		try {
			//2.The Process of the deSerializing
			FileInputStream fileInputStream = new FileInputStream("D:\\JavaCodes\\Examps\\JasonWord.txt");
			ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
			SerializeInstance instance02 = (SerializeInstance)objectInputStream.readObject();
			System.out.println(instance02.num + "WuLiWaLa");
			
		}catch (IOException | ClassNotFoundException e) {
			 e.printStackTrace();
		}
		System.out.println("Hello World! ");
	}
	
}
Supplementary knowledge: What is the use of serialVersionUID?
The last part of this blog explains the use of serialVersionUID and code verification.
Here is a brief summary: serialVersionUID is the serialized version number of a class. Classes that implement the Serializable interface must have this field (meaning either manually specify one , Or in case of warning, the compiler will generate one by default, anyway, there must be one), and it must be of private static final long data type. what's the point? It is like a key, only corresponding to a certain object, it needs to be decrypted when deserializing.
In order to improve the independence and certainty of serialVersionUID, it is strongly recommended to define serialVersionUID in a serializable class and assign it a clear value.

Explicitly defining serialVersionUID has two purposes:
    1. In some occasions, it is hoped that different versions of the class are compatible with serialization, so it is necessary to ensure that different versions of the class have the same serialVersionUID;
    2. In some occasions, you do not want the same serialVersionUID Different versions are compatible with serialization, so you need to ensure that different versions of the class have different serialVersionUIDs.
    



In the writing process of this article, I mainly refer to the following blog content. Welcome everyone to refer to it:
1. Only find a method for success, not an excuse for failure: https://www.cnblogs.com/xdp-gacl/p/3777987 .html
2. Feifeifei: https://www.cnblogs.com/gtaxmjld/p/4866931.html

Guess you like

Origin blog.csdn.net/qq_37040173/article/details/83896049