Professional Documents
Culture Documents
Serialization
Serialization
Serialization
bject into
-------------- a byte stream.
2-i.e reading the data from object and writing to file.
3-It is mainly used in Remote computer ,Hibernate, RMI, JPA(Java
Persistence API), EJB technologies.
note->
A remote computer is a computer to which a user does not have physical access, b
ut which he or she can access or manipulate via some kind of network.
Remote desktop software allows a person to control a computer from another compu
ter; this allows the user to change anything on the linear computer, and access
all of the file contents.
De-Serialization->1-The reverse operation of serialization is called deserializa
tion.
------------------2-it is the process of reading the data from file and creating
object on remote m/c.
note->The String class and all the wrapper classes implements java.io.Serializab
le interface by default.
common use of Serialization->
---------------------------1-It is mainly used to travel object's state on the network
2-if you want to store the data of an object in secured format so that it can be
accessed by authorised java application only.
3-By using the Serialization you can manage the memory available required for yo
ur java application
4-if you have the requirement to transfer the object data from one m/c to anothe
r m/c through the network.
**subhag m/c
________________________
** nidhi m/c
_______________
| class Add
| { int sid
String name
| ------------
main()
______
|
|
r1.txt file
|
--
bhag"|
| {
|------------------------------>|
|
|
a1,a2; // object |
| a2-| 102,"ra
dhe" |
|
|______________
|
|
_____|
|______________________|
note->1- int this diagram i am creating a java program in subhag m/c and in thi
s program i am creating two object and i want to save the object data into the n
idhi m/c. so this thing can be acheived by Serialization.
2- in the same m/c you want to persist(keep the data permanently) the sta
te of an object you can do with the help of serialization.
1-so to pass the object from one m/c to another m/c you have to save the state o
f an object to the file
2-state of the object means the current value of instance variable
**3-you can use the following classes for this purpose.
(a)ObjectOutputStream
(b)ObjectInputStream
**4-To save the state of an Object to file you can use void writeObject(Object)
method of(Serialization)
ObjectOutputStream
**5-To create the object from file on remote m/c you can use Object readObject()
method of(Deserialization)
ObjectInputStream.
6-By default any objects are not eligible for serailization so if you want to se
rialize any class object then that class has to imlement java.io.Serializable
marker interface.
----------------------------------------------------------------------------------------Serializable interface->
----------------------1-this interface comes from java.io package.
1-Serializable is a marker interface (has no method decelartion).
It is just used to "mark" java classes which support a certain capability.
2-It must be implemented by the class whose object state you want to keep .
ex->
import java.io.Serializable;
class Student implements Serializable
{
int id;
String name;
Student(int id, String name)
{
this.id = id;
this.name = name;
}
}
ObjectOutputStream class
-----------------------The ObjectOutputStream class is used to write primitive data types and Java obje
cts to an OutputStream. Only objects that support the java.io.Serializable inter
face can be written to streams.
ex-1
import java.io.*;
{
Object obj = ois.readObject();
Object obj1 = ois.readObject();
System.out.println(obj);
System.out.println(obj1);
}
System.out.println("object Deserialized");
}
}
{
public static void main(String args[])throws Exception
{
ObjectInputStream in=new ObjectInputStream(new FileInputStream("f.txt"));
Student s=(Student)in.readObject();
System.out.println(s.id+" "+s.name+" "+s.age);
in.close();
}
}
o/p->211 ravi 0
------------------------------------------------------------------------------------------------------------------Externalization->
---------------1- Externalization is the process of customizing serialization process.
2- by using Externalization concept you can serialize particular fields of your
class.
3- to perform Externalization,your class has to be implement java.io.Externaliza
tion interface.
4- this is not a marker interface like java.io.Serializable interfae. this conta
ins following
two abstract method:--> void readExternal(ObjectInput)
--> void writeExternal(ObjectOutput)
5- writeExternal() method will take ObjectOutput stream as a parameter and insid
e this method
you have to specify which fields of your class want to serialize.
6- readExternal() method will take ObjectInput Stream as a parameter and inside
this method
you have to deserialize particuler field of your class.
7- writeExternal() will be called while serializing the object.
8- readExternal() will be called while deserializing the object.
9- your class must be have public no-argumented constructor.
import java.io.*;
class Student implements Externalizable
{
int sid;
String name ;
long phone;
String add;
public Student()
{
}
public Student(int sid,String name,long phone,String add)
{
this.sid=sid;
this.name=name;
this.phone=phone;
this.add=add;
}
public String toString()
{
return sid+"\t"+name+"\t"+phone+"\t"+add;
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(name);
out.writeObject(add);
out.writeLong(phone);
}
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
name = in.readObject().toString();
add=in.readObject().toString();
phone = in.readLong();
}
}
import java.io.*;
public class Demo1 {
public static void main(String[] args)throws Exception {
FileInputStream fis = new FileInputStream("G://abs12.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
{
Object obj = ois.readObject();
System.out.println(obj);
System.out.println("Object Deserialized");
}
}
}
for deserialization->
--------------------import java.io.*;
public class Demo1 {
public static void main(String[] args)throws Exception {
FileInputStream fis = new FileInputStream("G://abs.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
{
Object obj = ois.readObject();
System.out.println(obj);
System.out.println("Object Deserialized");
}
}
}