Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday 16 August 2017

Serialization In Java


What is Serialization In Java?

Serialization In Java

Serialization in java is a mechanism to convert an object into a byte stream so that we can send over the network or save it as file or store into a database. Java serialization was introduced in JDK 1.1 and it is the most important feature of java.

Deserialization in java is just opposite of serialization in java. 

The main purpose of serialization in java is to converting an state of an object into a stream of bytes.

Here we will learn serialization and deserialization in java with example so that you can understand better.

Serialization is used in RMI, JPA, Hibernate etc.


Advantages of Serialization In Java

  1. It is mainly used to travel the object on the network.
  2. The entire process is JVM independent because object serialized on one platform and deserialized on an entirely different platform.


java.io.Serializable Interface

A class must implement java.io.Serializable interface to be eligible for serialization.

For example:

import java.io.Serializable;
class Student implements Serializable
{
int rollno;
String name;
Student(int rollno, String name)
{
this.rollno = rollno;
this.name = name;
}
}

In the above example, Student class implements Serializable interface . Now the object of the Student class can be converted into stream.

In java ObjectOutputStream and ObjectInputStream both are high-level stream that contains methods for serialization and deserialization of an object.


Java ObjectOutputStream Class

It is used to write primitive data types and objects to an output stream. Only objects that supports Serializable interface.

Methods of ObjectOutputStream

There are some methods of this class.

(1) public final void writeObject(Object obj)throws IOException 

This method writes the specified object to the ObjectOutputStream.

(2) public void flush() throw IOException

This method flushes the current output stream.

(3) public void close() throw IOException

This method closes the current output stream.



Java Serialization Example

In this class, we are going to serialize the object of Student class by the help of writeObject() method of ObjectOutputStream class because it provides the functionality to serialize the object. We will save the data of an object in a particular file name anu.txt.

import java.io.Serializable;
import java.io.*;
class Sample
{
public static void main(String args[])throws Exception
{
Student s = new Student(32, "amir");
FileOutputStream fout = new FileOutputStream(" anu.txt ");
ObjectOutputStream out = new ObjectOutputStream(fout);

out.writeObject(s);
out.flush();
System.out.println("task complete");
}
}

Output: task complete


Deserialization In Java

Deserialization in java is used to converting stream to object. Java deserialization is opposite of serialization.

Java ObjectInputStream Class

This class is used to deserialized the object of a class.

Methods of ObjectInputStream

There are some methods of output stream class which will help in deserializing an object.

(1) public final void readObject()throws IOException, ClassNotFoundException

This method read the object from the input stream.

(2) public void close()throws IOException

This method close object input stream.


Java Deserialization Example

This is simple example of deserialization of an object.

import java.io.Serializable;
import java.io.*;
class Sample1
{
public static void main(String args[])throws Exception
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("anu.txt"));
Student ss = (Student)ois.readObject();
System.out.println(ss.rollno+" "+ss.name);
ois.close();
}

}

Output: 32 amir


Share:

1 comment:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate