Latest web development tutorials

Java serialization

Java provides an object-serialization mechanism, which, an object can be represented as a sequence of bytes, the byte sequence includes the type of the object's data, information about the object and the type of data stored in the object .

After the file is written to a serialized object can be read out from the file, and it is deserialized, that is, the type of object information, data objects, and object data types can be used in memory the new objects.

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.

ObjectInputStream and ObjectOutputStream class high-level data streams, serialization and methods they contain anti-sequence objects.

ObjectOutputStream class contains many write method to write a variety of data types, but a special way exceptions:

public final void writeObject(Object x) throws IOException

The above method serialize an object and send it to the output stream. Similar ObjectInputStream class contains the following deserialize an object's method:

public final Object readObject() throws IOException, 
                                 ClassNotFoundException

This method takes the next object from the stream, and the object is deserialized. Its return value is Object, so you need to convert it to the appropriate data type.

To demonstrate the Java serialization is how it works, I will use the Employee class tutorial mentioned before, if 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 a class object serialization in order to succeed, two conditions must be met:

The class must implement java.io.Serializable object.

All such attributes must be serializable. If there is a property is not serializable, the property must be stated briefly.

If you want to know whether a Java standard class is serializable, see the documentation for this class. Test whether an instance of a class can be serialized is very simple, just need to see the class has not achieved java.io.Serializable interface.


Serialized object

ObjectOutputStream class is used to serialize an object, SerializeDemo following example instantiates an Employee object, and the object is serialized to a file.

After this program is executed, it creates a file named employee.ser. This program has no output, but you can read the code to understand the role of a program.

Note: When an object is serialized to a file, in accordance with the standard Java convention is to 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();
      }
   }
}

Deserialize objects

The following program example DeserializeDemo deserialization, / tmp / employee.ser stored 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);
    }
}

Compile and run the above program results are as follows:

Deserialized Employee...
Name: Reyan Ali
Address:Phokka Kuan, Ambehta Peer
SSN: 0
Number:101

Here we must note the following points:

readObject () method in a try / catch block attempts to catch ClassNotFoundException exception. For the JVM can deserialize object, it must be able to find the bytecode classes. If the JVM can not find the class in the deserialization process object, throw a ClassNotFoundException exception.

Note that the return value, readObject () method is converted to Employee reference.

When an object is serialized, the value of property SSN 111 222 333, but because the property is short, the value is not sent to the output stream. So SSN attribute deserialized Employee object is 0.