Serialization and Deserialization Tutorials in Java:-

12
Serialization means convert object data to Stream(file) and deserialization means Convert Stream(file)  to Object.
Stream means the Sequence of bytes.
for example, if we store student data into student objects and we want to write Student object data to file then we will use data serialization.
If we have Student data in the file and we want to co-relate file data to object then we can use de-serialization.
Java provides a Serializable Interface to implement Serialization this interface does not contain any method it only contains behavior hence It is also called Marker Interface.
Java Provide ObjectOuputStream Class and ObjectInputStream Class to Write and Read Data into Object.
 Complete Example of Serialization and De-serialization?
import java.io.*;
class Student implements Serializable
{
   int rno;
   String sname;
   Student(int rno,String sname)
   {
      this.rno=rno; 
      this.sname=sname;
   } 
}
class SerExample
{
     public static void main(String args[])  throws

IOException,ClassNotFoundException
     {
         File f = new File("mno.txt");
         /*Student obj = new Student(1001,"xyz");
        
         FileOutputStream fo = new FileOutputStream(f);
         ObjectOutputStream oo = new ObjectOutputStream(fo);
         oo.writeObject(obj);  // object to stream
         fo.close(); */
         Student obj1=null;
         FileInputStream fi = new FileInputStream(f);
         ObjectInputStream oi = new ObjectInputStream(fi);
         obj1 = (Student)oi.readObject();   // stream to object
         System.out.print(obj1.rno+" ,"+obj1.sname);            
     }

Another 
Note:-  What is a transient keyword in Java?
It is used to protect the variable from serialization for example if we do not want to serialize a particular data member of the class then we can use transient keyword
Now we are taking three variable rno and name is the instance variable and the fee is a transient variable that can not be serialized.
import java.io.Serializable;

/**
 *
 * @author Hp
 */
public class Student implements Serializable {
    int rno;
    String sname;
    transient int fees;
    Student(int rno,String sname,int fees)
    {
        this.rno = rno;
        this.sname=sname;
        this.fees = fees;
    }       
}
Write the code for data serialization and deserialization:-
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
 *
 * @author Hp
 */
public class SerializationExample {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Student obj = new Student(1001,"xyz",45090);
        FileOutputStream fo = new FileOutputStream("D://studentser.txt");
        ObjectOutputStream oo = new ObjectOutputStream(fo);
        oo.writeObject(obj);
        oo.close();
        Student obj1 = null;
        FileInputStream fi = new FileInputStream("D://studentser.txt");
        ObjectInputStream oi = new ObjectInputStream(fi);
        obj1 =(Student) oi.readObject();
        System.out.print("rno is "+obj1.rno + " name is  "+obj1.sname + "Fees is "+obj1.fees);
        fi.close();
           }    
}
Another Example of Serialization and DeSerialization:-
import java.io.*;
class Student implements Serializable
{
 transient int rno=10;
 String sname;
 void accept(int rno,String sname)
 {
    this.rno=rno;
    this.sname=sname;
 } 
 void display()
 {
    System.out.println("rno is "+rno + " name is "+sname);
 }      
}
class SerializationMain
{
static void ser() throws IOException
{
    FileOutputStream fo = new FileOutputStream("stu.txt");
   ObjectOutputStream oo = new ObjectOutputStream(fo);
   Student s = new Student();
   s.accept(1001,"XYZ");
   oo.writeObject(s);
   fo.close();   
}
static void deser() throws IOException, ClassNotFoundException
{
   FileInputStream fo = new FileInputStream("stu.txt");
   ObjectInputStream oo = new ObjectInputStream(fo);
   Student s = null;
   s = (Student)oo.readObject();
   s.display();
   fo.close();   
}
public static void main(String args[]) throws IOException,ClassNotFoundException
{
   ser();
   deser();
}
}
package filehandlingexample;
import java.io.Serializable;
public class Emp implements Serializable {
private int empid;
private String empname;
Emp(int empid,String empname)
{
this.empid=empid;
this.empname=empname;
}
public String toString()
{
return "Empid is "+empid+ "Name is "+empname;
}
}
package filehandlingexample;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SeraializationExample {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Emp obj = new Emp(1001,"Nitin Kumar");
FileOutputStream fo = new FileOutputStream("d://empser.txt");
ObjectOutputStream oo = new ObjectOutputStream(fo);
oo.writeObject(obj);
oo.close();
FileInputStream fi = new FileInputStream("d://empser.txt");
ObjectInputStream oi = new ObjectInputStream(fi);
Emp obj1 = null;
obj1 =(Emp)oi.readObject();
System.out.println(obj1);
}
}
Tags

Post a Comment

12Comments

POST Answer of Questions and ASK to Doubt

  1. Another example of Serialization and Deserialization?

    import java.io.*;
    class SI implements Serializable
    {
    float p,r,t,si;
    SI(float p,float r, float t)
    {
    this.p=p;
    this.r=r;
    this.t=t;
    }
    }

    class SIMain
    {
    public static void main(String args[]) throws Exception
    {
    /* FileOutputStream fo = new FileOutputStream("si.txt");
    SI obj = new SI(45000,2,2);
    obj.si = (obj.p*obj.r*obj.t)/100;
    ObjectOutputStream oo = new ObjectOutputStream(fo);
    oo.writeObject(obj);
    oo.close(); */
    SI s=null;
    FileInputStream fo = new FileInputStream("si.txt");
    ObjectInputStream oo = new ObjectInputStream(fo);
    s =(SI)oo.readObject();
    System.out.println(s.p + " "+s.r + " "+s.t + " "+s.si);
    oo.close();

    }

    }

    ReplyDelete
  2. import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutput;
    import java.io.ObjectOutputStream;

    public class SerializationExample {

    public static void main(String[] args) throws IOException {
    FileOutputStream fo = new FileOutputStream("d://ser.txt");
    Student s = new Student(1001,"XYZ");
    ObjectOutputStream oo = new ObjectOutputStream(fo);
    oo.writeObject(s);
    oo.close();
    fo.close();


    }

    }

    ReplyDelete
  3. import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;

    public class Deserialization {

    public static void main(String[] args) throws ClassNotFoundException, IOException {
    FileInputStream fo = new FileInputStream("d://ser.txt");
    Student s = null;
    ObjectInputStream oo = new ObjectInputStream(fo);
    s= (Student)oo.readObject();
    System.out.println(s);
    oo.close();
    fo.close();

    }

    }

    ReplyDelete
  4. import java.io.Serializable;

    public class Student implements Serializable {
    private int rno;
    private transient String sname;
    Student(int rno,String sname)
    {
    this.rno=rno;
    this.sname=sname;
    }
    public String toString()
    {
    return "rno is "+rno +" name is "+sname;
    }
    }

    ReplyDelete
  5. //yogesh seroke
    //program to serialization-deserialization
    import java.io.*;
    class Student implements Serializable
    {
    String rn;
    String name;
    void accept(String rn,String name)
    {
    this.rn=rn;
    this.name=name;
    }

    }

    class Serializationmain
    {
    public static void main(String args[]) throws IOException,ClassNotFoundException
    {
    Student obj=new Student();
    obj.accept("1001","yogesh seroke");
    File f=new File("c://yogeshserokeserializationdeserialization//sede.txt");
    FileOutputStream fo=new FileOutputStream(f);
    ObjectOutputStream oo=new ObjectOutputStream(fo);

    oo.writeObject(obj); //object to stream
    oo.close();

    FileInputStream fi=new FileInputStream(f);
    ObjectInputStream oi=new ObjectInputStream(fi);

    obj=(Student)oi.readObject();
    System.out.println("roll number is "+obj.rn+" name is "+obj.name);
    }
    }

    ReplyDelete
  6. //yogesh seroke
    //simple interest using serialization-deserialization
    import java.io.*;
    class SimpleInterest implements Serializable
    {
    int p,r,t,si;
    void accept(int p,int r, int t)
    {
    this.p=p;
    this.r=r;
    this.t=t;
    }
    void logic()
    {
    si=(p*r*t)/100;
    }

    }

    class SImain
    {
    public static void main(String args[]) throws IOException , ClassNotFoundException
    {
    SimpleInterest obj=new SimpleInterest();
    obj.accept(50000,3,2);
    obj.logic();

    File f=new File("c://yogeshserokeserializationdeserialization//sede1.txt");
    FileOutputStream fo=new FileOutputStream(f);
    ObjectOutputStream oo=new ObjectOutputStream(fo);
    oo.writeObject(obj); //object to stream
    oo.close();

    FileInputStream fi=new FileInputStream(f);
    ObjectInputStream oi=new ObjectInputStream(fi);
    obj=(SimpleInterest)oi.readObject();
    System.out.println("amount = "+obj.p+"\n"+"rate = "+obj.r+"\n"+"time = "+obj.t+"\n"+"simple interest is = "+ obj.si);
    }
    }

    ReplyDelete
  7. //yogesh seroke
    //program to find even/odd number using serialization-deserialization

    import java.io.*;
    import java.util.Scanner;
    class Prime implements Serializable
    {
    int num;
    void accept(int num)
    {
    this.num=num;
    }
    void logic()
    {
    if(num%2==0)
    {
    System.out.println("number is even");
    }
    else
    {
    System.out.println("number is odd");
    }
    }
    }

    class Primemain
    {
    public static void main(String args[]) throws IOException, ClassNotFoundException
    {
    Scanner sc = new Scanner(System.in);
    Prime obj =new Prime();
    System.out.println("enter number...");
    int num=sc.nextInt();
    obj.accept(num);
    obj.logic();

    File f=new File("c://yogeshserokeserializationdeserialization//sede2.txt");
    FileOutputStream fo=new FileOutputStream(f);
    ObjectOutputStream oo=new ObjectOutputStream(fo);
    oo.writeObject(obj);

    FileInputStream fi=new FileInputStream(f);
    ObjectInputStream oi=new ObjectInputStream(fi);
    obj=(Prime)oi.readObject();
    System.out.println(obj.num);
    }
    }

    ReplyDelete
  8. //yogesh seroke
    //transient keyword...

    import java.io.*;
    class Student1 implements Serializable
    {
    String rn,name;
    transient String fees;

    void accept(String rn,String name,String fees)
    {
    this.rn=rn;
    this.name=name;
    this.fees=fees;
    }
    }
    class Student1main
    {
    public static void main(String args[]) throws IOException , ClassNotFoundException
    {
    Student1 obj=new Student1();
    obj.accept("1001","yogesh seroke","25000");

    File f=new File("c://yogeshserokeserializationdeserialization//sede3.txt");
    FileOutputStream fo=new FileOutputStream(f);
    ObjectOutputStream oo=new ObjectOutputStream(fo);
    oo.writeObject(obj);
    oo.close();

    FileInputStream fi=new FileInputStream(f);
    ObjectInputStream oi=new ObjectInputStream(fi);
    obj=(Student1)oi.readObject();
    System.out.println("roll number is = "+obj.rn+"\n"+"name is = "+obj.name+"\n"+"fees is = "+obj.fees);
    }
    }

    ReplyDelete
  9. //yogesh seroke
    //transient keyword

    import java.io.*;
    class SI1 implements Serializable
    {
    int p,r;
    transient int t;
    int si;
    void accept(int p,int r,int t)
    {
    this.p=p;
    this.r=r;
    this.t=t;
    }
    void logic()
    {
    si=(p*r*t)/100;
    }
    }

    class SI1main
    {
    public static void main(String args[])throws IOException, ClassNotFoundException
    {
    SI1 obj=new SI1();
    obj.accept(50000,3,2);
    obj.logic();

    File f=new File("c://yogeshserokeserializationdeserialization//sede4.txt");
    FileOutputStream fo=new FileOutputStream(f);
    ObjectOutputStream oo=new ObjectOutputStream(fo);
    oo.writeObject(obj);
    oo.close();

    FileInputStream fi=new FileInputStream(f);
    ObjectInputStream oi=new ObjectInputStream(fi);
    obj=(SI1)oi.readObject();
    System.out.println(obj.si);
    System.out.println(obj.t);
    oi.close();
    }
    }

    ReplyDelete
  10. //yogesh seroke
    //transient keyword

    import java.io.*;
    import java.util.Scanner;
    class Prime1 implements Serializable
    {
    int num1;
    transient int num2;

    void accept(int num1,int num2)
    {
    this.num1=num1;
    this.num2=num2;
    }
    void logic()
    {
    if(num1%2==0)
    {
    System.out.println("even");
    }
    else
    {
    System.out.println("odd");
    }
    if(num2%2==0)
    {
    System.out.println("even");
    }
    else
    {
    System.out.println("odd");
    }
    }
    }

    class Prime1main
    {
    public static void main(String args[]) throws IOException , ClassNotFoundException
    {
    Scanner sc=new Scanner(System.in);
    Prime1 obj=new Prime1();
    System.out.println("enter first number = ");
    int num1=sc.nextInt();
    System.out.println("enter second number = ");
    int num2=sc.nextInt();
    obj.accept(num1,num2);
    obj.logic();

    File f=new File("c://yogeshserokeserializationdeserialization//sede5.txt");
    FileOutputStream fo=new FileOutputStream(f);
    ObjectOutputStream oo=new ObjectOutputStream(fo);
    oo.writeObject(obj);
    oo.close();

    FileInputStream fi=new FileInputStream(f);
    ObjectInputStream oi=new ObjectInputStream(fi);
    obj=(Prime1)oi.readObject();
    System.out.println("first number is = "+ obj.num1);
    System.out.println("second number is = "+ obj.num2);
    }
    }

    ReplyDelete
  11. package filehandlingexample;

    import java.io.Serializable;

    public class SIexample implements Serializable {
    private float p;
    private float r;
    private float t;
    float si;


    public float getP() {
    return p;
    }
    public void setP(float p) {
    this.p = p;
    }
    public float getR() {
    return r;
    }
    public void setR(float r) {
    this.r = r;
    }
    public float getT() {
    return t;
    }
    public void setT(float t) {
    this.t = t;
    }
    public float getSi() {
    return si;
    }
    public void setSi(float si) {
    this.si = si;
    }
    /*SIexample(float p,float r,float t)
    {
    this.p=p;
    this.r=r;
    this.t=t;
    }*/
    void accept(float p,float r,float t)
    {
    this.p=p;
    this.r=r;
    this.t=t;
    }
    void siLogic()
    {
    si=(p*r*t)/100;
    }
    public String toString()
    {
    return "Result is "+si;

    }
    }

    ReplyDelete
  12. package filehandlingexample;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;

    public class SiMain {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
    SIexample obj = new SIexample();
    //obj.accept(12000, 2, 2);
    obj.setP(12000);
    obj.setR(2);
    obj.setT(2);
    obj.siLogic();
    FileOutputStream fo = new FileOutputStream("d://simplesi.txt");
    ObjectOutputStream oo = new ObjectOutputStream(fo);
    oo.writeObject(obj);
    oo.close();
    FileInputStream fi = new FileInputStream("d://simplesi.txt");
    ObjectInputStream oi = new ObjectInputStream(fi);
    SIexample obj1 = null;
    obj1 =(SIexample)oi.readObject();
    System.out.println(obj1.getP() + " "+obj1.getR() + " "+ obj1.getT());

    }

    }

    ReplyDelete
Post a Comment