Serializing and Deserializing an Object In Java

 Posted On  | Yashwant Chavan 

Object Serialization is used to maintain an Object State. Object serialization converts an object state into a binary format which can be persisted in file.To make a Java object serializable you implement the java.io.Serializable interface.

Java Serialization(SerializationExample.java

java.io.Serializable Interface is a marker interface which mark as an object to serializable. In below example we are going to persist the state of employee object. in the "employee.ser". You can deserialised the byte stream into original employee object.

java.io.NotSerializableException: com.technicalkeeda.bean.Employee
 at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
 at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
 at com.technicalkeeda.bean.SerializationExample.main(SerializationExample.java:18)

Incase if you forgot to implement the Serializable interface then you will get the above big fat NotSerializableException exception

package com.technicalkeeda.bean;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class SerializationExample {

 public static void main(String[] args) {
  try {
   Employee employee = new Employee();
   employee.setEmployeeId(1);
   employee.setFirstName("Yashwant");
   employee.setLastName("Chavan");
   FileOutputStream fos = new FileOutputStream("C:\\employee.ser");
   ObjectOutputStream oos = new ObjectOutputStream(fos);
   oos.writeObject(employee);
   oos.flush();
   oos.close();
  } catch (FileNotFoundException e) {
   System.out.println("FileNotFoundException:- " + e.getMessage());
  } catch (IOException e) {
   System.out.println("IOException :- " + e.getMessage());
  }
 }

}

Java Deserialization(DeserializationExample.java

In serialization you can convert the original object to byte stream and Deserialization you can convert the buyte stream to original object. In below code we rae going to Deserialized the employee.ser file to employee object.

package com.technicalkeeda.bean;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

public class DeserializationExample {

 public static void main(String[] args) {
  try {
   FileInputStream fis = new FileInputStream("c:\\employee.ser");
   ObjectInputStream ois = new ObjectInputStream(fis);
   Employee employee = (Employee) ois.readObject();
   ois.close();
   System.out.println("Employee Id:- " + employee.getEmployeeId());
   System.out.println("First Name:- " + employee.getFirstName());
   System.out.println("Last Name:- " + employee.getLastName());
  } catch (FileNotFoundException e) {
   System.out.println("FileNotFoundException .." + e.getMessage());
  } catch (IOException e) {
   System.out.println("IOException .." + e.getMessage());
  } catch (ClassNotFoundException e) {
   System.out.println("ClassNotFoundException .." + e.getMessage());
  }
 }

}

Finish!

Hope this article is helpful to you, Please provide your comments if any thing is required


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0305 seconds.