Friday, November 9, 2012

Java Serialization and Deserialization

Here is a small example of Java's serialization and deserialization

File : Student.java

package com.giri.serial;

import java.io.Serializable;

public class Student implements Serializable {
 private String name;
 private int age;
 private float percentage;
 private String college;
 
 public Student() {
  
 }

 public Student(String name, int age, float percentage, String college) {
  super();
  this.name = name;
  this.age = age;
  this.percentage = percentage;
  this.college = college;
 }

 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 float getPercentage() {
  return percentage;
 }

 public void setPercentage(float percentage) {
  this.percentage = percentage;
 }

 public String getCollege() {
  return college;
 }

 public void setCollege(String college) {
  this.college = college;
 }
 
 public void print() {
  System.out.println("\n[Student] Name:"+name+ " Age:"+age + " Percentage:"+percentage+ " College:"+college);
 }
 
 
}

File : Serialize.java


package com.giri.serial;

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

public class Serialize {
 
 private Student student;
 private String filename;
 
 public Serialize() {
  student = null;
  filename = null;
 }
 
 public Serialize(Student student, String filename) {
  super();
  this.student = student;
  this.filename = filename;
 }

 public Student getStudent() {
  return student;
 }

 public void setStudent(Student student) {
  this.student = student;
 }

 public String getFilename() {
  return filename;
 }

 public void setFilename(String filename) {
  this.filename = filename;
 }
 
 public void serialize() {
  FileOutputStream fos = null;
  ObjectOutputStream oos = null;
  
  try {
   fos = new FileOutputStream(filename);
   oos = new ObjectOutputStream(fos);
   oos.writeObject(student);
   oos.close();
   fos.close();
  } catch(Exception e) {
   e.printStackTrace();
  }
 }
}

File : Deserialize.java

package com.giri.serial;

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

public class Deserialize {
 
 private String filename;
 
 public Deserialize() {
  filename = null;
 }
 
 public Deserialize(String f) {
  filename = f;
 }

 public String getFilename() {
  return filename;
 }

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

File : SerializableMainClass.java

package com.giri.serial;

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

public class SerializableMainClass {

 public static void main(String[] args) {
  
  String filename = "student.ser";
  FileOutputStream fos = null;
  ObjectOutputStream oos = null;
  
  
  
  Student s = new Student();
  s.setName("Bill Gates");
  s.setCollege("Microsoft University");
  s.setAge(25);
  s.setPercentage(75);
  
  System.out.println("(1) Serialization ..");
  
  Serialize ser = new Serialize(s,filename);
  ser.serialize();
  
  System.out.println("(2) Deserialization ..");
  
  Deserialize deser = new Deserialize(filename);
  Student new_student = deser.deserialize();
  new_student.print();
  
 }
}

Output

(1) Serialization ..
(2) Deserialization ..

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

[Student] Name:Bill Gates Age:25 Percentage:75.0 College:Microsoft University

No comments :

Post a Comment