Friday, November 9, 2012

Java Serialization and Deserialization using Generics

Here is a small example of Java's serialization and deserialization through the use of Generics

File : MyData.java

package com.giri.generics;

import java.io.Serializable;

public class MyData implements Serializable {
 private String name;
 private int age;
 private String country;
 
 
 public MyData(String name, int age, String country) {
  super();
  this.name = name;
  this.age = age;
  this.country = country;
 }
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public String getCountry() {
  return country;
 }
 public void setCountry(String country) {
  this.country = country;
 }
 
 public void print() {
  System.out.println("\n [MyData] : " + "Name => "+ name + "  Age => " + age + "  Country => " + country);
 }
 
}

File : SerializeGeneric.java

package com.giri.generics;

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

public class SerializeGeneric<T> {
 private String fileName;
 private T serData;
 
 public SerializeGeneric(String fileName, T serData) {
  super();
  this.fileName = fileName;
  this.serData = serData;
 }

 public String getFileName() {
  return fileName;
 }

 public void setFileName(String fileName) {
  this.fileName = fileName;
 }

 public T getSerData() {
  return serData;
 }

 public void setSerData(T serData) {
  this.serData = serData;
 }
 
 
 public void serialize() {
  FileOutputStream fos = null;
  ObjectOutputStream oos = null;
  
  try {
   fos = new FileOutputStream(fileName);
   oos = new ObjectOutputStream(fos);
   oos.writeObject(serData);
   oos.close();
   fos.close();
  } catch(Exception e) {
   e.printStackTrace();
  }
 }
}

File : DeserializeGeneric.java

package com.giri.generics;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class DeserializeGeneric<T> {
 
 private String fileName;

 
 public DeserializeGeneric(String fileName) {
  super();
  this.fileName = fileName;
 }

 public String getFileName() {
  return fileName;
 }

 public void setFileName(String fileName) {
  this.fileName = fileName;
 }
 
 public T deserialize() {
  System.out.println("\n Reading the file " + fileName + " and calling deserialize(): ");
  T data = null;
  try {
   FileInputStream fis = new FileInputStream(fileName);
   ObjectInputStream ois = new ObjectInputStream(fis);
   data = (T) ois.readObject();
   ois.close();
   fis.close();
  } catch(Exception e) {
   e.printStackTrace();
  }
  return data;
 }
 
}

File : MainClass.java

package com.giri.generics;

public class MainClass {


 public static  <E> void printArray( E[] inputArray ) {
  int count = 0;
  for(E element : inputArray ) {
   System.out.printf("\n inputArray[%d] = %s ", count++, element);
  }
  System.out.println("\n");
 }
 
 public static void main(String[] args) {
  
  String fileName = "serialization.ser"; 
    
  Integer[] intArray = { 1,2,3,4,5,6,7,8 };
  Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5 };
  Character[] charArray = { 'J', 'A', 'V', 'A' };
  
  System.out.println("\nInteger Array:");
  printArray(intArray);
  
  System.out.println("\nDouble Array:");
  printArray(doubleArray);
  
  System.out.println("\nCharacter Array:");
  printArray(charArray);
  
  MyData myData = new MyData("Giridhar", 22, "india");
  
  System.out.println("\n 1. Doing serialization ..");
  
  SerializeGeneric<MyData> ser_gen = new SerializeGeneric<MyData>(fileName, myData);
  ser_gen.serialize();
  
  System.out.println("\n 2. Doing De-serialization ..");
  DeserializeGeneric<MyData> deser_gen = new DeserializeGeneric<MyData>(fileName);
  MyData new_data = deser_gen.deserialize();
  new_data.print();
  
 }

}

Output


Integer Array:

 inputArray[0] = 1 
 inputArray[1] = 2 
 inputArray[2] = 3 
 inputArray[3] = 4 
 inputArray[4] = 5 
 inputArray[5] = 6 
 inputArray[6] = 7 
 inputArray[7] = 8 


Double Array:

 inputArray[0] = 1.1 
 inputArray[1] = 2.2 
 inputArray[2] = 3.3 
 inputArray[3] = 4.4 
 inputArray[4] = 5.5 


Character Array:

 inputArray[0] = J 
 inputArray[1] = A 
 inputArray[2] = V 
 inputArray[3] = A 


 1. Doing serialization ..

 2. Doing De-serialization ..

 Reading the file serialization.ser and calling deserialize(): 

 [MyData] : Name => Giridhar  Age => 22  Country => india

No comments :

Post a Comment