Original Address: http://www.work100.net/training/java-serialization.html
More Tutorials: beam cloud - free course
Serialization
No. | Article section | video |
---|---|---|
1 | Outline | |
2 | Serialized object | |
3 | Deserialized object |
Please refer above 章节导航
to read
1 Overview
There is provided a Java object serialization mechanism, which, an object can be represented as a sequence of bytes, the byte sequence comprising the object's data type, the type information about an object stored in the object data .
After writing the serialized object file can be read out from the file, and it is deserialized, i.e., the type information of the object, data object, and the data type objects can be used in memory in the newly created object.
The whole process is a Java Virtual Machine (JVM) independent, that is, on a platform serialized object can deserialize the object on a completely different platform.
Class ObjectInputStream
and ObjectOutputStream
high-level data stream, which comprises a method and deserialize serialized objects.
ObjectOutputStream
Class contains many write method to write a variety of data types, but a special exception to the methodology:
public final void writeObject(Object x) throws IOException
The above method of serializing an object, and sends it to the output stream. Similar ObjectInputStream
class comprising the deserializing an object's method:
public final Object readObject() throws IOException,
ClassNotFoundException
This method fetches the next object from the stream, and the object deserialization. Its return value Object
, so you need to convert it to the appropriate data type.
To demonstrate serialization in Java how it works, I will use the previously mentioned tutorial Employee
classes, suppose we define the following Employee
class that 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);
}
}
Please note that a class object serialization in order to succeed, two conditions must be met:
The class must implement java.io.Serializable
the interface.
All properties of the class must be serializable. If a property is not serializable, the property must indicate short-lived.
If you want to know whether it is a Java standard class can be serialized, please view the document class. Test if an instance of a class can be serialized very simple, only need to see the class has not implemented java.io.Serializable
the interface.
2. serialized object
ObjectOutputStream
Class is used to serialize an object, the following SerializeDemo
example of an instance of Employee
the object, and the object is serialized to a file.
After the program is executed, it creates a named employee.ser
file. The program has no output, but you can understand the role of the program by reading the code.
Note: When you serialize an object to a file, in accordance with the standard Java convention is to file an .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. deserialized object
The following DeserializeDemo
program example deserialization, /tmp/employee.ser
stored Employee
objects.
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 results compiled to run the above procedure is as follows:
Deserialized Employee...
Name: Reyan Ali
Address:Phokka Kuan, Ambehta Peer
SSN: 0
Number:101
Here we must note the following points:
readObject()
The method of try/catch
code blocks try to capture ClassNotFoundException
exception. For JVM can deserialize objects, it must be able to find the class bytecode. If the JVM can not find the class in the process of anti-sequence objects, then throw an ClassNotFoundException
exception.
Note that, readObject()
the return value is converted to a Employee
reference.
When an object is serialized, the attribute SSN
value 111222333
, but because the property is short, this value is not transmitted to the output stream. So after deserialization Employee
object SSN
property 0
.
Previous: Generic
Next: Network Programming