Recent Posts

Wednesday, 17 August 2016

File I/O & Serialization Tutorial Part 2


Serialization
     The process of saving (or) writing state of an object to a file is called "Serialization". But strictly speaking it is the process of converting an object from java supported form to either network supported form (or) file supported form. By using FileOutputStream and ObjectOutputStream classes we can achieve Serialization process.


DeSerialization
     The process of reading state of an object from a file is called "DeSerialization". But strictly speaking it is the process of converting an object from file supported form (or) network supported form to java supported form. By using FileInputStream and ObjectInputStream classes we can achieve DeSerialization.

E.g
package com.ashok.files;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Ashok implements Serializable {
   String name = "Ashok";
   String website = "http://www.waytoeasylearn.com";
}

public class MySerialization {
   public static void main(String arg[]) throws Exception {
      ObjectOutputStream oos = null;
      FileInputStream fis = null;
      ObjectInputStream ois = null;
      try {
         Ashok ashok1 = new Ashok();
         System.out.println("Serialization started");
         FileOutputStream fos = new FileOutputStream("abc.ser");
         oos = new ObjectOutputStream(fos);
         oos.writeObject(ashok1);
         System.out.println("Serialization ended");
   
         System.out.println("Deserialization started");
         fis = new FileInputStream("abc.ser");
         ois = new ObjectInputStream(fis);
         Ashok ashok2 = (Ashok) ois.readObject();
         System.out.println("Deserialization ended");
         System.out.println(ashok2.name + " ------> " + ashok2.website);
      } catch(Exception e) {
         e.printStackTrace();
      } finally {
         if(null != oos) {
            oos.close();
         }
         if(null != fis) {
            fis.close();
         }
         if(null != ois) {
            ois.close();
         }
      }
   }
}
Output
Serialization started
Serialization ended
Deserialization started
Deserialization ended
Ashok................http://www.waytoeasylearn.com
     An Object is said to be serializable if and only if the corresponding class should implement serializable interface. serializable interface present in java.io package and doesn’t contain any method, it is marker interface.

     If you are trying to perform serialization of a non serialization Object. we will get runtime exception saying NonSerializableException.

     If we don’t want to Serialize the value of a particular variable (To meet security constraints) we should declare those variables as transient. While performing serialization JVM ignores the value of transient variables and saves default values instead of original values.

    static variables are not part of object state and hence they won’t participate in serialization process. Declaring static variables as transient there is no impact similarly declaring final variables as transient creates no impact.


Serialization in the case of Object Graphs
     When ever we are saving an object to a file all the objects which are reachable from that object will be saved by default. This group of objects is called ‘Object Graph’.

     In the Object Graph if any Object is non-Serialzable we will get runtime Exception saying NotSerializableException.
E.g
package com.ashok.files;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Dog implements Serializable {
   Cat c = new Cat();
}

class Cat implements Serializable {
   Rat r = new Rat();
}

class Rat implements Serializable {
   String name = "Welcome to Waytoeasylearn.com";
}

public class MySerialization {
   public static void main(String arg[]) throws Exception {
      Dog d = new Dog();
      FileOutputStream fos = new FileOutputStream("ashok.ser");
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(d);
      FileInputStream fis = new FileInputStream("ashok.ser");
      ObjectInputStream ois = new ObjectInputStream(fis);
      Dog d1 = (Dog) ois.readObject();
      System.out.println(d1.c.r.j);
   }
}
Output
Welcome to Waytoeasylearn.com
     In the above program among Dog, Cat and Rat classes if any class is not Serializable we will get runtime Exception saying java.io.NotSerializableException.

Customized Serialization
     During default Serialization there may be a chance of loss of information because of transient variables.

E.g
package com.ashok.files;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Dog implements Serializable {
   Cat c = new Cat();
}
class Cat {
   String name = "Welcome to Waytoeasylearn.com";
}

public class MySerialization {
   public static void main(String arg[]) throws Exception {
      Dog d = new Dog();
      FileOutputStream fos = new FileOutputStream("ashok.ser");
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(d);
      FileInputStream fis = new FileInputStream("ashok.ser");
      ObjectInputStream ois = new ObjectInputStream(fis);
      Dog d1 = (Dog) ois.readObject();
      System.out.println(d1.c.r.j);
   }
}
Output
Exception in thread "main" java.io.NotSerializableException: com.ashok.files.Cat
     We can implement customized Serialization by using the following two methods.
private void writeObject(OutputStream os)throws Exception {
   ..........
   ..........
}
     This method will be executed automatically by the JVM at the time of Serialization.
private void readObject(InputStream is)throws Exception {
   ..........
   ..........
}
     This method will be executed automatically by the JVM at the time of deSerialization.
E.g
package com.ashok.files;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Dog implements Serializable {
   transient Cat c = new Cat();

   private void writeObject(ObjectOutputStream os) throws IOException {
      int x = c.j;
      os.writeInt(x);
   }

   private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
      is.defaultReadObject();
      int k = is.readInt();
      c = new Cat();
      c.j = k;
   }
}

class Cat {
   int j = 100;
}

class SerializeDemo {
   public static void main(String arg[]) throws Exception {
      Dog d = new Dog();
      System.out.println("Before Serialization:" + d.c.j);
      FileOutputStream fos = new FileOutputStream("ashok.ser");
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(d);
      FileInputStream fis = new FileInputStream("ashok.ser");
      ObjectInputStream ois = new ObjectInputStream(fis);
      Dog d1 = (Dog) ois.readObject();
      System.out.println(d1.c.j);
   }
}
Output
100

Next Tutorial  File I/O & Serialization Tutorial Part 3

Previous Tutorial  File I/O & Serialization Tutorial Part 1

No comments:

Post a Comment