Serialization and Deserialization in Java with Example

  • Serialization is a mechanism of converting a java object into byte stream.
  • Deserialization is reverse of serialization in which byte stream is converted back to a java object in memory.
  • Byte stream created is platform independent so, the object serialized on one platform can be deserialized on other platform.
  • For making java object serializable we must have to implements java.io.Serializable interface.
  • We can serialize an object from writeObject() method which is defined in ObjectOutputStream class.
  • We can deserialize an object from readObject() method which is defined in ObjectInputStream class.


    Serialization and De-Serialization Example :-

import java.io.*; 
class Emp implements Serializable { 
 private static final long serialversionUID =129348938L; 
 transient int a; 
 int b; 
 String name; 
 int age; 
 public Emp(String name, int age, int a, int b) 
 { 
  this.name = name; 
  this.age = age; 
  this.a = a; 
  this.b = b; 
 } 
}

public class SerialExample { 
   public static void printdata(Emp object1) 
   { 
     System.out.println("name = " + object1.name); 
     System.out.println("age = " + object1.age); 
     System.out.println("a = " + object1.a); 
     System.out.println("b = " + object1.b); 
   } 
   public static void main(String[] args) 
   { 
     Emp object = new Emp("ab", 20, 2, 1000); 
     String filename = "shubham.txt"; 
     // Serialization 
     try { 
           // Saving of object in a file 
           FileOutputStream file = new FileOutputStream(filename); 
           ObjectOutputStream out = new ObjectOutputStream(file); 
           // Method for serialization of object 
           out.writeObject(object); 
           out.close(); 
           file.close(); 
           System.out.println("Object has been serialized\n"
            + "Data before Deserialization."); 
           printdata(object); 
           // value of static variable changed 
           object.b = 2000; 
      } 
      catch (IOException ex) 
      { 
           System.out.println("IOException is caught"); 
      }
      object = null; 
      // Deserialization 
      try { 
            // Reading the object from a file 
            FileInputStream file = new FileInputStream(filename); 
            ObjectInputStream in = new ObjectInputStream(file); 
            // Method for deserialization of object 
            object = (Emp)in.readObject(); 
            in.close(); 
            file.close(); 
            System.out.println("Object has been deserialized\n"
              + "Data after Deserialization."); 
            printdata(object); 
            // System.out.println("z = " + object1.z); 
       } 
       catch (IOException ex) { 
            System.out.println("IOException is caught"); 
       } 
       catch (ClassNotFoundException ex) { 
            System.out.println("ClassNotFoundException" + 
              " is caught"); 
       } 
    } 
}
Share:

No comments:

Post a Comment

Quote of the day

Popular Posts

Featured Post

Collection Framework Overview

Collection:- A collection (sometimes called a container) is simply an object that groups multiple elements into single unit. Collect...

Youtube Page

Facebook Page

Recent Posts

About

I am Shivaji Chandra and I'm a Computer Science Engineer. I love to write poems, jokes and quotes. click here →