SERIALIZATION:
·
When we use the normal streams to read and write
the data to and from the file, there is a possibility for performing unnecessary
read and write operations because of this there is a possibility of loosing the
performance of java program.
·
In order to improve the performance of java
program by perform single read and write operation irrespective number of
fields we must use the concept of Serialization.
·
DEFINITION: Serialization is a mechanism of
saving the state of the entire object at a time on to a file.
·
Steps for developing serializable subclass:
o
Choose the appropriate package name.
o
Choose the appropriate user defined serializable
sub class name.
o
Whichever class we have chosen in previous step
must be implementing a predefined interface call java.io.Serializable.
Serializable interface does not contain any abstract method i.e.
Marker/Marked/Tagged interface.
o
Choose the set of data members in serializable
sub class.
o
Choose the set of setR() methods for each and
every data member of the serializable sub class. The set of setR() methods are
also known as mutators or modifiers whose general form is as follows:
public void setXXX
(data-type variable)
{
}
Where XXX represents
the data member of serializable sub class
o Choose
the set of getR() methods for each and every data member of the serializable
sub class. The set of getR() methods are also known as inspectors whose general
form is as follows:
public data-type
getXXX()
{
}
Where XXX represents
the data member of the serializable sub class
Note: If any class is containing
set of getR() and setR() methods then that class is known as javaBean or
POJO(Plain Old Java Object) class and such type of programming is known as
Component Based Style Programming.
o
Develop a serializable sub class for Student.
package p;
Import java.io.*;
public class Student
implements Serializable
{
private int stNo;
private
String stName;
private
float stMarks;
public void setStNo(int stNo)
{
this.stNo=StNo;
}
public
void setStName(int stName)
{
this.stName=StName;
}
public
void setMarks(int marks)
this.marks=Marks;
}
public int getStNo()
{
return(stNo);
}
public String
getStName()
{
return(stName);
}
public float getStMarks()
{
return(marks);
}
}
o Develop
a Serialization process program:
import java.io.*;
class
SerializationProcessDemo
{
public static void main(String args[])
throws Exception
{
DataInputStream dis=new
DataInputStream(System.in);
p.Student
s=new p.Student();
System.out.println(“Enter
Student # : ”);
int
rollnum=Interger.parseInt(dis.readLine());
System.out.println(“Enter
Student Name : ”);
String sName=dis.readLine();
System.out.println(“Enter
Student Marks:”);
float
marks=Float.parseFloat(dis.readLine());
s.setStNo(rollNumber);
s.setStName(sName);
s.setMarks(marks);
System.out.println(“Enter
the filename where you want to write the object: ”);
String
fName=dis.readLine();
FileOutputStream
fos=new FileOutputStream(fName);
ObjectOutputStream
oos=new ObjectOutputStream(fos);
oos.writeObject(s);
dis.close();
fos.close();
oos.close();
}
}
DESERIALIZATION:
·
It is a mechanism of retrieving the records from
the file into the main memory of the computer or it is the ability of retrieving
one stream object into the main memory of the computer is known as
Deserialization.
·
STEPS FOR DESERIALIZATION PROCESS:
(1)
Create an object of serializable subclass.
(2)
Choose the file name and open it into read mode
with the help of FileInputStream class.
(3)
An object of FileInputStream class will not read
the entire record at once hence it is recommended to create an object of ObjectInputStream
class. ObjectInputStream class contains the following constructor which takes
an object of FileInputstream class as a parameter.
ObjectInputStream (FileInputStream)
(4)
ObjectInputStream class contains the following
method which will read the entire object at a time from the file and it returns
an object of Object class.
public Object readObject();
(5)
Type cast the object of Object class into
corresponding serializable subclass object for calling specific methods of
serializable subclass.
(6)
Call set of getR() to get the data from
serializable subclass object.
(7)
Close the file which is opened in read mode and
close all stream.
Sample Program:
import java.io.*;
class DeserializationDemo
{
public static void
main(String aa[])
{
try
{
p.Student s =new
p.Student();
DataInputStream
dis=DataInputStream (System.in);
System.out.println(“Enter
the filename you want to read from : ”);
String
fName=dis.readLine();
FileInputStream
fis=new FileInputStream(fName);
ObjectInputStream
ois=new ObjectInputStream(fis);
Object obj =
ois.readObject();
s = (Student)obj; //type casting
System.out.println(“Student
# : ”+s.getStNo());
System.out.println(“Student
Name : ”+s.getStName());
System.out.println(“Student
Marks : ”+s.getStMarks());
}
catch(FileNotFoundException fe)
{
e.printStackTrace();
}
}
NOTE: In order to develop a complete Serializable application, we must
develop 3 programs-
(1)
Serializable subclass program [Student.java]
(2)
Serialization process program [SerializationProcess.java]
(3)
Deserialization process program
[DeserializationDemo.java]
===================================================