Java Serialization is a process of object serialization. In Java serialization, an object is represented as a sequence of bytes. This sequence of bytes is called byte stream. This sequence of bytes hold information about the object. This information includes the data of object and further type of that data. The purpose of Java serialization is to convert an object into bytes and revert the byte stream into the object form when needed.
Java Deserialization
The process of reverting the byte stream to object is called deserialization.
The classes like ObjectOutputStream and ObjectInputStream have certain methods present in them that provide the mechanisms of object serialization and deserialization. These are the two methods in the ObjectOutputStream and ObjectInputStream classes that write and read different types of object respectively.
- public final void writeObject(Object obj) throws IOException {}: This method serializes the object and writes it to the output stream.
- public void close() throws IOException {}: This method closes the current output stream.
- public final Object readObject() throws IOException, ClassNotFoundException{}: This method deserializes the byte stream and returns the object.
- public void close() throws IOException {}: This method closes the current input stream.
The syntax of using these methods in code is:
Here is an example that defines a class. The class implements java.io.serialization. It is mandatory for a class to implement java.io.serialization class in order to perform serialization. The below code defines a Book class:
public class Book implements java.io.Serializable { public String name; public String author; public int price; public int pages; public void mailCheck() { System.out.println("The book " + name + " is written by " + author); } }
The above code has no output.
Serialization of an Object
The below code shows how to serialize an object of class Book and then write the serialized data to a file. Note that only non-static data members can saved through Serialization process. The static data members and transient data members can not be saved through Serialization process.
import java.io.*; public class Serialization { public static void main(String [] args) { Book b = new Book(); // Object of class Book b.name = "Programming Fundamentals"; b.author = "Kenneth Leroy Busbee"; b.price = 110; b.pages = 801; try { FileOutputStream fileOut = new FileOutputStream("/tmp/book.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(b); // Writes serialized data to a file out.close(); fileOut.close(); System.out.printf("Serialized data is saved in the file /tmp/book.ser"); } catch (IOException i) { i.printStackTrace(); } } }
The above code has to output to display, It creates a file named as book.ser at the desired location. This file contains the serialized data of the object.
Deserialization of an Object
The below code shows how to deserialize the object of class Book created in the above code. The program reads the serialized data from the file and deserializes it into the attributes of the object Book.
import java.io.*; public class Deserialization { public static void main(String [] args) { Book b = null; try { FileInputStream fileIn = new FileInputStream("/tmp/book.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); b = (Book) in.readObject(); in.close(); fileIn.close(); } catch (IOException i) { i.printStackTrace(); return; } catch (ClassNotFoundException c) { System.out.println("Book class not found"); c.printStackTrace(); return; } System.out.println("Deserialized Book contents are..."); System.out.println("Book Name: " + b.name); System.out.println("Author Name: " + b.author); System.out.println("Price in $: " + b.price); System.out.println("Number of Pages: " + b.pages); } }
In the above code, the try catch blocks are used to handle exceptions like ClassNotFoundException. This exception is declared in the readObject() method. The return value of readObject() method is saved in an object of class Book. The output of the above code after compilation an execution is:
Advantages of Java Serialization
The main advantage of Java serialization is networking where it helps transfer information among networks. This process is called marshalling. Moreover, Java Serialization helps to maintain the state and information of an object. In this way it becomes easier to send the object over different networks without having to worry about the persistence of the state of of objects.