·  JAVA – Syntax – Terms – Serialization

 

- Info:

  - Serialization is process of storing objects data as file on hard disk.

  - This way data will not get lost once the application exits.

  - Deserialization is process of loading objects data from file into memory.

  - To make custom class serializable just add implements Serializable.

 

- Vector:

  - In this example built in class Vector, which implements Serializable, is Serialized and Deserialized.

  - Create JAVA Console Application Test.java

 

    Test.java

    import java.util.Vector;

    import java.io.IOException;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.ObjectInputStream;

    import java.io.ObjectOutputStream;

    

    public class Test {

    

      public static void main(String[] args) throws IOException, ClassNotFoundException {

        

        //CREATE OBJECT.

        Vector v = new Vector();

               v.add("First");

               v.add("Second");

    

        //SERIALIZE OBJECT. Save object to file.          

        FileOutputStream   fout = new FileOutputStream("test.dat");

        ObjectOutputStream oos  = new ObjectOutputStream(fout);

                           oos.writeObject(v);

                           oos.close();

      

        //DELETE VECTOR DATA.

        v.clear();

                            

        //DESERIALIZE OBJECT. Load object from file.         

        FileInputStream    fin = new FileInputStream("test.dat");

        ObjectInputStream  ois = new ObjectInputStream(fin);

        v = (Vector)       ois.readObject();

                           ois.close();

      

        //DISPLAY VECTOR DATA.

        System.out.println(v.get(0));

        System.out.println(v.get(1));

    

      }

    

    }


- Custom Class:

  - In this example custom class will be Serialized and Deserialized.

  - Create JAVA Console Application Test.java

 

    Test.java

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.ObjectInputStream;

    import java.io.ObjectOutputStream;

    import java.io.Serializable;

    

    public class Test{

      

      public static void main(String[] args) throws ClassNotFoundException, IOException {

        

        //CREATE OBJECT.

        SerializeMe         sm  = new SerializeMe();

                            sm.i=100;

        

        //SERIALIZE OBJECT. Save object to file.

        ObjectOutputStream  out = new ObjectOutputStream(new FileOutputStream("SerializeMe.dat"));

                            out.writeObject("Storage procedure.");

                            out.writeObject(sm);

                            out.close();

    

        //DESERIALIZE OBJECT. Load object from file.  

        ObjectInputStream   in  = new ObjectInputStream(new FileInputStream("SerializeMe.dat"));

        String              s   = (String)      in.readObject(); //Without this Exception is thrown

        SerializeMe         sm2 = (SerializeMe) in.readObject();      

        

        //DISPLAY.

        System.out.println(s);     //Storage procedure.

        System.out.println(sm2.i); //100

      }

    }

    

    //CLASS WHICH OBJECT WILL BE SERIALLIZED.

    class SerializeMe implements Serializable{

      int i;

    }

 

    SerializeMe.dat

    _)..t.Storage procedure.sr.SerializMEl:"čćšđs

 

- Errors:

  - Exception in thread "main" java.lang.ClassCastException: java.lang.String at Test.main(Test.java:25)

  - This error occurs if you do not use following line during deserialization of custom class:

    String s = (String) in.readObject();