Java provides an object serialization mechanism in which an object can be represented as a sequence of bytes that 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, the type information of the object, the data of the object, and the data type of the object can be used in the memory. New object.
The entire process is independent of the Java Virtual Machine (JVM), which means that objects serialized on one platform can be deserialized on a completely different platform.
The 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 the exception of a special method:
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 methods that deserialize an object as follows:
public final Object readObject ( ) throws IOException ,
ClassNotFoundException
This method takes the next object from the stream and deserializes the object. 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 that implements the Serializable interface.
Employee.java file code:
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 for a class object to be serialized successfully, two conditions must be met:
This class must implement a java.io.Serializable object.
All properties of this class must be serializable. If there is an attribute that is not serializable, the attribute must be marked as transient.
If you want to know if a Java standard class is serializable, check the documentation for that class. It is very simple to check whether an instance of a class can be serialized, just check whether the class implements the java.io.Serializable interface.
Serialized object
The ObjectOutputStream class is used to serialize an object. The following SerializeDemo example instantiates an Employee object and serializes the object to a file.
After the program is executed, a file called employee.ser is created. The program does not have any output, but you can understand the role of the program through code reading.
Note: When serializing an object to a file, the Java convention is to give the file a .ser extension.
SerializeDemo.java file code:
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();
}
}
}
Serialized data is saved in /tmp/employee.ser
Deserialize objects
The DeserializeDemo program instance below deserializes and /tmp/employee.ser stores the Employee object.
DeserializeDemo.java file code:
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 above program compiles and runs as follows:
Deserialized Employee... Name: Reyan Ali Address:Phokka Kuan, Ambehta Peer SSN: 0 Number:101
Here are the key points to note:
The try/catch code block in the readObject() method attempts to catch a ClassNotFoundException exception. For the JVM to deserialize the object, it must be a class that can find the 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 the 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.
Comments
Post a Comment