Serialization in Java

 


Serialization is a important topic in Java where we will use to Serialize the Object into a file or database or even in memory buffer and can transmitted across the network. Serialized Object can be de-serialized at any time and can get same state of original Object as same as cloning the Object. 

If we say in simple words Serialization is streaming Java object to a sequence of byte and restoring same objects from the stream. For example we need to save the state of Object at run-time we can use Serialization and store the Object state in a file. If same Object state need to be used in future then we can de-derialization and can make use of the Object.

Basically for implementing Serialization we need to implement java.io.Serializable interface in the class where the Objects get Serialized. In below example we have implemented in MySerializable class where this class Objects get Serialized. Serialization can't be used in various class where we use Threads, Socket etc.,

While we Serialize complete class variables will get Serialized. Suppose if we feel some variables should not be Serialized then we need to mark those variables as transient as like we have made in our below example. Those variables will not get serialized and will give null while when we de-serialize those variables. 

Below are the simple example for Serialization where we are serializing MySerializable class Object into a file and again de-serializing same Object back. Also we have used transient for "company" variable which won't get Serialized.



public class MySerializable implements Serializable {

 private static final long serialVersionUID = 1023;
 
 private int id;
 private String name;
 private String gender;
 transient private String company; // Variable will not to be Serialized
 
 public MySerializable(int id, String name, String gender, String company){
  this.id = id;
  this.name = name;
  this.gender = gender;
  this.company = company;
 }
 
 public int getId() {
  return id;
 }
 public String getName() {
  return name;
 }
 public String getGender() {
  return gender;
 }
 public String getCompany() {
  return company;
 }
 }




public class SerializationSample {

 public static void main(String args[])  {
  try{
   
   MySerializable serialB = new MySerializable(101, "Steve", "Male", "XYZ Inc.,");
   
   // Serializing the Object and storing in a file
   serialize("C:\\serial.out", serialB);
   System.out.println("Serialization completed...");
   
  }catch (Exception e) {
   e.printStackTrace();
  }
 }

 public static void serialize(String file, Object seriObj)
  throws IOException {
  FileOutputStream fos = new FileOutputStream(file);
  ObjectOutputStream oos = new ObjectOutputStream(fos);
  oos.writeObject(seriObj);
 } 
}


OUTPUT:

Serialization completed...




public class DeSerializableSample {
 
 public static void main(String[] args) {
  try{
   
   // De-serializing the Object from the file
   MySerializable obj = (MySerializable) deSerialize("C:\\serial.out");
   
   System.out.println("ID      : "+obj.getId());
   System.out.println("NAME    : "+obj.getName());
   System.out.println("GENDER  : "+obj.getGender());
   System.out.println("COMPANY : "+obj.getCompany());
   
  }catch (Exception e) {
   e.printStackTrace();
  }
  
 }
 
 public static Object deSerialize(String seriObj)
  throws FileNotFoundException, IOException, ClassNotFoundException {
  FileInputStream fis = new FileInputStream(seriObj);
  ObjectInputStream ois = new ObjectInputStream(fis);
  return ois.readObject();
 }
}


OUTPUT:

ID      : 101
NAME    : Steve
GENDER  : Male
COMPANY : null






No comments:
Write comments