Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it
preface
Tip: the following is the main content of this article. The following cases can be used for reference
https://www.cnblogs.com/xiohao/p/4234184.html
1, The role of Java serialization
Sometimes we want to transfer a Java object into a byte stream, and sometimes we want to restore a Java object from a byte stream. For example, sometimes we want to write a Java object to the hard disk or transfer it to other computers on the network. At this time, we need to write the corresponding object into a byte stream through Java.
Why don't we use a unified format for this common operation? Yes, the concept of Java serialization appears here. In the subclass ObjectOutputStream under the OutputStream class of Java, the ObjectOutputStream class has a corresponding WriteObject(Object object), in which the corresponding object is required to implement the serialization interface of Java.
In order to better understand the application of java serialization, I give two examples I have encountered in development projects:
1) When using Tomcat to develop Java EE related projects, after we close tomcat, the objects in the corresponding session are stored on the hard disk. If we want to read the contents of the corresponding session from Tomcat when Tomcat restarts, the contents saved in the session must be serialized.
2) If the java object we use is to be used in distributed or rmi remote call network, the relevant object must implement java serialization interface.
Dear friends, you probably know the functions related to Java serialization. Next, let's take a look at how to realize java serialization
2, Realize the serialization and deserialization of java objects
1. There are two ways to serialize Java objects.
a. The corresponding object implements the Serializable interface, which is often used. For the Serializable interface, the Serializable interface is an empty interface. Its main function is to identify the Serializable object. The jre object will be encapsulated when transmitting the object. I won't make too much introduction here.
The following is an example of Java serialization that implements the serialization interface: very simple
package com.shop.domain; import java.util.Date; public class Article implements java.io.Serializable { private static final long serialVersionUID = 1L; private Integer id; private String title; //Article title private String content; // Article content private String faceIcon;//Emoticon private Date postTime; //Time of publication private String ipAddr; //User's ip private User author; //Users replying public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getFaceIcon() { return faceIcon; } public void setFaceIcon(String faceIcon) { this.faceIcon = faceIcon; } public Date getPostTime() { return postTime; } public void setPostTime(Date postTime) { this.postTime = postTime; } public User getAuthor() { return author; } public void setAuthor(User author) { this.author = author; } public String getIpAddr() { return ipAddr; } public void setIpAddr(String ipAddr) { this.ipAddr = ipAddr; } }
b. The second way to realize serialization is to realize the interface Externalizable. Part of the source code of Externalizable is as follows:
* @see java.io.ObjectInput * @see java.io.Serializable * @since JDK1.1 */ public interface Externalizable extends java.io.Serializable { /** * The object implements the writeExternal method to save its contents * by calling the methods of DataOutput for its primitive values or
Yes, the Externlizable interface inherits the serialization interface of java and adds two methods:
void writeExternal(ObjectOutput out) throws IOException; void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;
First of all, when serializing objects, because this class implements the Externalizable interface and defines which attributes can be serialized and which cannot be serialized in the writeExternal() method, we save the serialized files that can be serialized after the object passes through here, and those that cannot be serialized will not be processed,
Then, when the sequence is reversed, the readExternal() method is automatically called, the sequence is read one by one according to the sequence sequence, and the sequence is automatically encapsulated into an object return, and then received in the test class to complete the reverse sequence.
Therefore, exterminable is an extension of Serializable.
In order to better understand the relevant contents, please see the following examples:
package com.xiaohao.test; import java.io.Externalizable; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.text.SimpleDateFormat; import java.util.Date; /** * Test entity class * @author Xiao Hao * @Creation date: March 12, 2015 */ class Person implements Externalizable{ private static final long serialVersionUID = 1L;<br> String userName; String password; String age; public Person(String userName, String password, String age) { super(); this.userName = userName; this.password = password; this.age = age; } public Person() { super(); } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } /** * Extension class for serialization operation */ @Override public void writeExternal(ObjectOutput out) throws IOException { //Add a new object Date date=new Date(); out.writeObject(userName); out.writeObject(password); out.writeObject(date); } /** * Deserialized extension class */ @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { //Note that the order of acceptance here is limited, otherwise it will make mistakes // For example, if the object A is written first above, then the object A must be accepted first below userName=(String) in.readObject(); password=(String) in.readObject(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date date=(Date)in.readObject(); System.out.println("The date after deserialization is:"+sdf.format(date)); } @Override public String toString() { //Note that the age here will not be serialized, so the data cannot be read during deserialization return "user name:"+userName+"password:"+password+"Age:"+age; } } /** * Related operation classes of serialization and deserialization * @author Xiao Hao * @Creation date: March 12, 2015 */ class Operate{ /** * Serialization method * @throws IOException * @throws FileNotFoundException */ public void serializable(Person person) throws FileNotFoundException, IOException{ ObjectOutputStream outputStream=new ObjectOutputStream(new FileOutputStream("a.txt")); outputStream.writeObject(person); } /** * Method of deserialization * @throws IOException * @throws FileNotFoundException * @throws ClassNotFoundException */ public Person deSerializable() throws FileNotFoundException, IOException, ClassNotFoundException{ ObjectInputStream ois=new ObjectInputStream(new FileInputStream("a.txt")); return (Person) ois.readObject(); } } /** * Test entity main class * @author Xiao Hao * @Creation date: March 12, 2015 */ public class Test{ public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { Operate operate=new Operate(); Person person=new Person("Xiao Hao","123456","20"); System.out.println("The relevant data before serialization is as follows:\n"+person.toString()); operate.serializable(person); Person newPerson=operate.deSerializable(); System.out.println("-------------------------------------------------------"); System.out.println("The relevant data after serialization is as follows:\n"+newPerson.toString()); } }
First of all, when serializing UserInfo objects, because this class implements the Externalizable interface and defines which attributes can be serialized and which cannot be serialized in the writeExternal() method, the serialization of objects that can be serialized will be saved in the file, and those that cannot be serialized will not be processed, Then, when the sequence is reversed, the readExternal() method is automatically called, the sequence is read one by one according to the sequence sequence, and the sequence is automatically encapsulated into an object return, and then received in the test class to complete the reverse sequence.
summary
Compared with the three methods of transmitting the same data, google protobuf has only 53 bytes, which is the least. Conclusion: