Serialization

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

Serialization->1-Serialization is the process of writing the (data)state of an o

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()

|------------------------------>| a1-| 101,"su

______
|
|
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.*;

class Student implements Serializable


{
int sid;
String name;
long phone;
Student(int sid,String name,long phone)
{
this.sid=sid;
this.name=name;
this.phone=phone;
}
public String toString()
{
return sid+"\t"+name+"\t"+phone;
}
}
public class Demo
{
public static void main(String args[]) throws Exception
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("G:\\abc.se
r"));
Student s1 = new Student(123,"subhag",961675407);
Student s2 = new Student(125,"abhay",888426315);
oos.writeObject(s1);
oos.writeObject(s2);
System.out.println(s1);
System.out.println(s2);
System.out.println("object serialized");
}
}
-------------------------------------------------------------------------------------------------------Deserialization in java->
----------------------Deserialization is the process of reconstructing the object from the serialized
state.It is the reverse operation of serialization.
ObjectInputStream class->
-----------------------An ObjectInputStream deserializes objects and primitive data written using an Ob
jectOutputStream.
ex-2-> now deserializing the Student class object
import java.io.*;
public class Demo1
{
public static void main(String args[]) throws Exception
{
FileInputStream fis = new FileInputStream("G:\\abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);

{
Object obj = ois.readObject();
Object obj1 = ois.readObject();
System.out.println(obj);
System.out.println(obj1);
}
System.out.println("object Deserialized");
}
}

without using toString() method->


-------------------------------ex-3
import java.io.*;
class Student implements Serializable
{
int sid;
String name;
long phone;
Student(int sid,String name,long phone)
{
this.sid=sid;
this.name=name;
this.phone=phone;
}
}
public class Demo
{
public static void main(String args[]) throws Exception
{
FileInputStream fis = new FileInputStream("G:\\info.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
{
Student s =(Student) ois.readObject();
Student s1 =(Student) ois.readObject();
System.out.println(s.sid+"\t"+s.name+"\t"+s.phone);
System.out.println(s1.sid+"\t"+s1.name+"\t"+s1.phone);
}
System.out.println("object Deserialized");
}
}

---------------------------------------------------------------------------------------------------------------Java Serialization with static data member->


------------------------------------------If there is any static data member in a class, it will not be serialized because
static is the part of class not object.

class Employee implements Serializable{


int id;
String name;
static String company;//it won't be serialized
public Student(int id, String name) {
this.id = id;
this.name = name;
}
}
-------------------------------------------------------------------------------------------------------------java transient Keyword->
---------------------1-Java transient keyword is used in serialization.
If you define any data member as transient, it will not be serialized.
2- it is a keyword in java that will be used with the varaible of the class(data
memeber).
3- this keyword is useful in the serailization process.if you are serializing
the object then by default all the instance variable will be srialized.
4- if you want to restrict that some variable should not serialized then those
variable can be defined as transient.
5- while deserializing the object these variables will get the default value
depending on data type.
6- if you do not want to serialize the member object then define as transient.
ex-4>
import java.io.Serializable;
public class Student implements Serializable
{
int id;
String name;
transient int age;//Now it will not be serialized
public Student(int id, String name,int age)
{
this.id = id;
this.name = name;
this.age=age;
}
public static void main(String args[])throws Exception{
Student s1 =new Student(211,"ravi",22);
FileOutputStream f=new FileOutputStream("f.txt");
ObjectOutputStream out=new ObjectOutputStream(f);
out.writeObject(s1);
out.flush();
out.close();
f.close();
System.out.println("success");
}
}
--->Now write the code for deserialization.
import java.io.*;
class Demo

{
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");
}
}
}

You might also like