CodesJava

Easy learning with example program codes

Serialization and Deserialization in java


In java serialization is way used to convert an object into a byte stream which can be transported to any other running JVM through a network or can be persisted into disk and that object can be rebuilt again. Java provides serialization API for this.

How to make a class serializable in java?

To make a java class Serializable implements java.io.Serializable interface. JVM will take care of serializing objects.

What is Serializable interface?

Serializable interface is a marker interface. A marker interface doesn’t have any method i.e. Serializable interface doesn’t have any method, it gives indication to compiler that use Java Serialization mechanism to serialize this object.

Example:

SerializationExample.java

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
 
/**
 * This program is used to show the serialization process.
 * @author codesjava
 */
class Student implements Serializable{
	//Serial Version UID.
	private static final long serialVersionUID = 1L;
	String name;
	String className;
	String rollNo;
 
	//Constructor.
	Student(String name, String className, String rollNo){
		this.name = name;
		this.className = className;
		this.rollNo = rollNo;
	}
}
 
class Test{
	//Write serialized object into objectoutputstream.
	public void objectSerialization(Student stu){
	   try
	      {
	         //Creating FileOutputStream object.
	         FileOutputStream fos = 
	          new FileOutputStream("F:\\New folder\\student.ser");
 
	         //Creating ObjectOutputStream object.
	         ObjectOutputStream oos = new ObjectOutputStream(fos);
 
	         //write object.
	         oos.writeObject(stu);
 
	         //close streams.
	         oos.close();
	         fos.close();
 
	         System.out.println("Serialized data is saved in " +
	         		"F:\\New folder\\student.ser");
	      }catch(IOException e)
	      {
	          System.out.println(e);
	      }
	}
}
 
public class SerializationExample {
	public static void main(String args[]){
		//Creating Student object.
		Student stu = 
                 new Student("Parmander", "MCA", "MCA/07/27");		
		//Creating Test object.
		Test obj = new Test();		
		//Method call.
		obj.objectSerialization(stu);		
	}
}

Output:

Serialized data is saved in F:\New folder\student.ser

Download this example.

Deserialization:

Deserialization is the reverse process of serialization. It is the process of rebuilding object from serialized state.

Example:

DeSerializationExample.java

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
 
/**
 * This program is used to show the deserialization process.
 * @author codesjava
 */
class Student implements Serializable{
	//Serial Version UID.
	private static final long serialVersionUID = 1L;
	String name;
	String className;
	String rollNo;
 
	//Constructor.
	Student(String name, String className, String rollNo){
		this.name = name;
		this.className = className;
		this.rollNo = rollNo;
	}
}
 
class Test{
	//Deserialize a serialize object.
	public void objectDeSerialization(){
	  try
	      {
		Student stu = null;
		//Creating FileOutputStream object.
		FileInputStream fis = 
	         new FileInputStream("F:\\New folder\\student.ser");
 
	         //Creating ObjectOutputStream object.
	         ObjectInputStream ois = new ObjectInputStream(fis);
 
	         //write object.
	         stu = (Student) ois.readObject();
 
	         //close streams.
	         ois.close();
	         fis.close();
 
	         System.out.println("Name = " + stu.name);
	         System.out.println("Class Name = " + stu.className);
	         System.out.println("RollNo = " + stu.rollNo);
	      }catch(Exception e)
	      {
	          System.out.println(e);
	      }
	}
}
 
public class DeSerializationExample {
	public static void main(String args[]){	
		//Creating Test object.
		Test obj = new Test();		
		//Method call.
		obj.objectDeSerialization();		
	}
}

Output:

Name = Parmander
Class Name = MCA
RollNo = MCA/07/27

Download this example.

What is the difference between Serializable and Externalizable interface in Java?

                     Serializable                           Externalizable
  1. Serializable interface is a marker interface.
  2. Serializable interface write state of an object in non-compress format.
  1. Externalizable interface is not a marker interface.
  2. Externalizable interface writes state of an object in compress format.

 
Next Topic: Transient in java with example.
Previous Topic: How To Check If A File Exists In Java.

Related Topics:

Input output (I/O) in java.
FileInputStream and FileOutputStream in java with example.
Byte Streams in java with example.
DataInputStream and DataOutputStream in java with example.
BufferedInputStream and BufferedOutputStream in java with example.
FileReader and FileWriter in java with example.
How To Check If A File Exists In Java.
Serialization in java with example.
Transient in java with example.

 

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki