Java Serialization with Externalizable and their difference

(120 Views)


Serialization in Java

Java provides a mechanism, called Object Serialization which allows user to store an Object's structure and data in an external file. This file contains information reagarding object; type of object, type of members and data held by those members.

This external file can have any extension but sticking to conventions, one should save this file with '.ser' extension.

An advantage of using Serialization is that you can later load this file into your java program and reload this object with its data in the memory.

An added advantage of serialization is that it is completely JVM independent which means you can write the '.ser' file on windows and later read it on linux or Mac.

Creating a serializable class:

A serializable class must implement java.io.Serializable interface. A member marked 'transient' will not have its data stored in '.ser' file. When we retrieve a transient member, it will have a default value of 0.

class Student implements Serializable{ public int rollNumber; public String name; public transient int marks; }

Serializing an object:

java.io.ObjectOutputStream class provides writeObject() method which lets us serialize an object to an external file. Serialization Example:

import java.io.*; public class Example{ public static void main(String[] args) throws FileNotFoundException, IOException { Student s = new Student(); s.rollNumber = 2; s.name = "Amit"; s.marks = 50; FileOutputStream fileOut = new FileOutputStream("F:/student.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(s); out.close(); fileOut.close(); } }

Deserialization:

Deserialization is a process of loading the content of '.ser' file back into the memory. java.io.ObjectInputStream class provides readObject() method which lets us deserialize an external file.

Deserialization example:

import java.io.*; class Student implements Serializable{ public int rollNumber; public String name; public transient int marks; } public class Example{ public static void main(String [] args) throws FileNotFoundException, IOException, ClassNotFoundException { Student s = null; FileInputStream file = new FileInputStream("F:/student.ser"); ObjectInputStream in = new ObjectInputStream(file); s = (Student) in.readObject(); in.close(); file.close(); System.out.println("rollNumber : "+s.rollNumber); System.out.println("Name : "+s.name); System.out.println("Marks : "+s.marks); } }

Externalizable interface:

Just like Serializable interface, Externizable also stores an object with its data to an external file which we can later retrieve. But there's some difference:

  • In serialization, all the members/variables in the object are serialized into the external file, whereas in Externalization, we can specifically mention which members do we want so be stored
  • Since serialization serialize whole class, we don't need to write methods for reading and writing the external file. But Externalizable requires two methods; namely readExternal() and writeExternal()
  • Externalization allows us to store different versions of members. For example oldRollNumber and newRollNumber.
  • Serializable class doesn't require a public no-arg constructor but it is mandatory in Extarnalizable class

Externalization example:

package testing; import java.io.*; class Student implements Externalizable { int rollNumber; String name; public Student(){ System.out.println("Default Constructor called"); } Student(String n, int r) { name = n; rollNumber = r; } public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(rollNumber); out.writeObject(name); } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { rollNumber = in.readInt(); name = (String)in.readObject(); } } public class Example { public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { Student car = new Student("Amit", 1); Student s = null; //Serialization FileOutputStream fo = new FileOutputStream("gfg.txt"); ObjectOutputStream so = new ObjectOutputStream(fo); so.writeObject(car); so.flush(); //Deserialization FileInputStream fi = new FileInputStream("gfg.txt"); ObjectInputStream si = new ObjectInputStream(fi); s = (Student)si.readObject(); } }

Solution Worked 0 UpvotesUpvote

        

Solution Didn't Worked 0 DownvotesDownvote



Comments



Search