Serialization Mechanism In Java

Posted By : Prakhar Verma | 26-Apr-2018

 

Serialization is a process of convert an object into a byte stream and travels it on the network.

Process for reading byte stream and convert it into an object is called deserialization.

Java provides "Serializable" interface for achieving serialization and it is located on "java.io" package.

Serializable is an abstract interface without fields and methods.

Serializable interface is only used for tell JVM (java virtual machine) that implementation class will be serialized.

Example => Create a simple class and implement Serializable interface.

package com.demo.domain;

import java.io.Serializable;
import java.util.Date;

public class Notification implements Serializable {

	private Integer notificationId;
	
	private String message;
	
	private Date createdAt;
	
	private boolean isRead;
	
	public Notification(){}
	
	public Integer getNotificationId() {
		return notificationId;
	}

	public void setNotificationId(Integer notificationId) {
		this.notificationId = notificationId;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Date getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}

	public boolean isRead() {
		return isRead;
	}

	public void setRead(boolean isRead) {
		this.isRead = isRead;
	}

Step 2 => Create a simple class with the main method for serializing notification object and write it into a file.

import java.io.*;
public class SerializeDemo {

   public static void main(String [] args) {
      Notification e = new Notification();
      e.notificationId = 1;
      e.message = "this is a serilization example";
      e.createdAt = "2018 Apr 28 11:22:55";
      e.isRead = false;
      
      try {
         FileOutputStream fileOut =
         new FileOutputStream("/tmp/notification.ser");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(e);
         out.close();
         fileOut.close();
         System.out.printf("Serialized data is saved in /tmp/notification.ser");
      } catch (IOException i) {
         i.printStackTrace();
      }
   }
}

Step 3 => Create a new class with the main method for deserializing notification object from a file.

import java.io.*;
public class DeserializeDemo {

   public static void main(String [] args) {
      Notification e = null;
      try {
         FileInputStream fileIn = new FileInputStream("/tmp/notification.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         e = (Notification) in.readObject();
         in.close();
         fileIn.close();
      } catch (IOException i) {
         i.printStackTrace();
         return;
      } catch (ClassNotFoundException c) {
         System.out.println("Notification class not found");
         c.printStackTrace();
         return;
      }
      
      System.out.println("Deserialized Notification...");
      System.out.println("Notification Id : " + e.notificationId);
      System.out.println("Message: " + e.message);
      System.out.println("Created At: " + e.createdAt);
      System.out.println("Is Read: " + e.isRead);
   }
}

Note => If any serialize object write into a file it will create with .ser extension 

 

About Author

Author Image
Prakhar Verma

Prakhar is a Web App Developer. Experienced in Java and always gives his best effort to complete the given task. He is self motivated and fun loving person.

Request for Proposal

Name is required

Comment is required

Sending message..