Original address: http://www.work100.net/training/java-serialization.html
More tutorials: Beam cloud - free course
serialize
Serial number | Chapter in text | video |
---|---|---|
1 | Summary | - |
2 | serialized objects | - |
3 | Deserialize object | - |
Please refer to the navigation above for reading
1. overview
Java provides a mechanism of object serialization, in which an object can be represented as a byte sequence, which includes the data of the object, information about the type of the object and the type of data stored in the object.
After the serialized object is written to the file, it can be read from the file and deserialized. That is to say, the type information of the object, the data of the object, and the data type of the object can be used to create a new object in memory.
The whole process is Java virtual machine (JVM) independent, that is, objects serialized on one platform can be deserialized on another completely different platform.
Classes ObjectInputStream and ObjectOutputStream are high-level data streams that contain methods for deserializing and serializing objects.
The ObjectOutputStream class contains many write methods to write various data types, with one special exception:
public final void writeObject(Object x) throws IOException
The above method serializes an object and sends it to the output stream. A similar ObjectInputStream class contains the following methods for deserializing an object:
public final Object readObject() throws IOException, ClassNotFoundException
This method takes the next Object from the stream and deserializes it. Its return value is Object, so you need to convert it to the appropriate data type.
To demonstrate how serialization works in Java, I will use the Employee class mentioned in the previous tutorial. Suppose we define the following employee class, which implements the Serializable interface.
public class Employee implements java.io.Serializable { public String name; public String address; public transient int SSN; public int number; public void mailCheck() { System.out.println("Mailing a check to " + name + " " + address); } }
Note that in order for an object of a class to be serialized successfully, two conditions must be met:
This class must implement the java.io.Serializable interface.
All properties of the class must be serializable. If there is a property that is not serializable, it must be noted that it is transient.
If you want to know if a Java standard class is serializable, check the documentation for that class. It's very simple to check whether an instance of a class can be serialized. You only need to check whether the class implements the java.io.Serializable interface.
2. Serialize objects
The ObjectOutputStream class is used to serialize an object. The following SerializeDemo example instantiates an Employee object and serializes the object into a file.
After the program executes, a file named employee.ser is created. The program has no output, but you can understand the function of the program by reading the code.
Note: when serializing an object to a file, the standard Java convention is to give the file a. ser extension.
import java.io.*; public class SerializeDemo { public static void main(String [] args) { Employee e = new Employee(); e.name = "Reyan Ali"; e.address = "Phokka Kuan, Ambehta Peer"; e.SSN = 11122333; e.number = 101; try { FileOutputStream fileOut = new FileOutputStream("/tmp/employee.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(e); out.close(); fileOut.close(); System.out.printf("Serialized data is saved in /tmp/employee.ser"); }catch(IOException i) { i.printStackTrace(); } } }
3. Deserializing objects
The following DeserializeDemo program instance deserializes, / tmp/employee.ser stores the Employee object.
import java.io.*; public class DeserializeDemo { public static void main(String [] args) { Employee e = null; try { FileInputStream fileIn = new FileInputStream("/tmp/employee.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); e = (Employee) in.readObject(); in.close(); fileIn.close(); }catch(IOException i) { i.printStackTrace(); return; }catch(ClassNotFoundException c) { System.out.println("Employee class not found"); c.printStackTrace(); return; } System.out.println("Deserialized Employee..."); System.out.println("Name: " + e.name); System.out.println("Address: " + e.address); System.out.println("SSN: " + e.SSN); System.out.println("Number: " + e.number); } }
The compilation and operation results of the above programs are as follows:
Deserialized Employee... Name: Reyan Ali Address:Phokka Kuan, Ambehta Peer SSN: 0 Number:101
The following points should be noted here:
The try/catch code block in the readObject() method attempted to catch a ClassNotFoundException exception. For a JVM to deserialize an object, it must be a class that can find bytecode. If the JVM cannot find the class during deserialization of the object, a ClassNotFoundException exception is thrown.
Note that the return value of the readObject() method is converted to an Employee reference.
When the object is serialized, the value of attribute SSN is 111222333, but because the attribute is transient, the value is not sent to the output stream. So the SSN property of the Employee object after deserialization is 0.
Last article: generic paradigm
Next article: Network programming
If you are interested in the content of the course, you can scan the code to pay attention to our official account or QQ group, and pay attention to our curriculum updates in time.