Java Tutorial

Serialization

Serialization is the process of saving the state of the object permanently in the form of a file.

Steps for developing serialization sub-class:

  1. Choose the appropriate package name.
  2. Choose the appropriate class name whose object is participating in serialization.
  3. Whichever class we have chosen in step 2 that must implements a predefined interface called java.io.Serializable (this interface does not contain any abstract methods and such type of interface is known as marked or tagged interface).
  4. Choose set of properties or data members of the class.
  5. Define set of setter methods (these are called mutators or modifiers).
  6. Define set of getter methods (these are also known as inspectors).

Write a JAVA program for student serializable sub-class?

Answer:

package sp;
import java.io.*;
public class Student implements Serializable
{
    int stno;
    String sname; 
    float marks;
    public void setSno (int stno)
    {
        this.stno=stno;
    }
    public void setSno (String sname)
    {
        this.sname=sname;
    }
    public void setSno (float marks)
    {
        this.marks=marks;
    }
    public int getStno ()
    {
        return (stno);
    }
    public String getSname ()
    {
        return (sname);
    }
    public float getMarks ()
    {
        return (marks);
    }
};

Serialization Process:

1. Create an object of serializable sub-class.

For Example:

Student so=new Student ();

2. Accept the data either from keyboard or from command prompt.

3. Call set of set methods to set the values for the serializable sub-class (Student) object.

For Example:

so.setSno (sno);

4. Choose the file name and open it into write mode with the help of File Output Stream class.

5. Since an object of File Output Stream class cannot write the entire object at a line to the file. In order to write the entire object at a time to the file we must create an object of ObjectOutputStream class and it contains the following Constructor:

ObjectOutputStream (FileOutputStream);

For Example:

ObjectOutputStream oos=new ObjectOutputStream (fos);

The object oos is pointing to object fos, hence such type of streams are called chained or sequenced stream.

6. Object Output Stream class contains the following instance method, which will write the entire object at a time to the file.

For example:

ObjectOutpurStream.writeObject (so);

Write a JAVA program to save or serialize student data?

Answer:

import java.io.*;
class StudentData
{
    public static void main (String [] args)
    {
        try
        {
            sp.Student so=new sp.Student ();
            DataInputStream dis=new DataInputStream (System.in);
            System.out.println ("ENTER STUDENT NUMBER : "); 
            int stno=Integer.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 ()); 
            so.setStno (stno);
            so.setSname (sname);
            so.setMarks (marks);
            System.out.println ("ENTER THE FILE NAME TO WRITE THE DATA");
            String fname=dis.readLine ();
            FileOutputStream fos=new FileOutputStream (fname);
            ObjectOutputStream oos=new ObjectOutputStream (fos); oos.writeObject (so);
            System.out.println ("STUDENT DATA IS SERIALIZED"); fos.close ();
            oos.close ();
        }
        catch (IOException ioe)
        {
            System.out.println ("PROBLEM IN CREATING THE FILE");
        }
        catch (Exception e)
        {
            System.out.println (e);
        }
    }
};