Core Java - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Serialization

Java Serialization

shape Description

Java Serialization, Serialization is a process of changing the present state of an object in the form of byte streams. After doing Java Serialization process one can read an object from the file, deserialization is used to recreate the exist state of an object. Here one interesting point is that total process is JVM independent .. To serialize or deserialize an object, a developer needs to use two classes as follows, these classes consist the complete methods of Java Serialization and deserialization. The ObjectOutputStream class will have write methods to serializes an object and sends it to the output stream, ObjectInputStream class also contains the method to deserialize an object as follows. [java]public final void writeObject(Object x) throws IOException[/java] [java]public final Object readObject() throws IOException, ClassNotFoundException[/java]

shape Conceptual Figure

Serialization

shape Description

Java Serialization, Following is an example to understand the concept of Java Serialization, here first developer is going to create one class with the name Employee as follows. Employee.java [java]package serialization; public class Employee implements java.io.Serializable { public String name; public String address; public transient int SSN; public int number; public void mailCheck() { System.out.println("Hello Splesson " + name + " " + address); } }[/java] While working with serialization then the class should implement java.io.Serializable interface. In the following example developer is going to assign the name, address. SerializeDemo.java [java]package serialization; import java.io.*; public class SerializeDemo { public static void main(String [] args) { Employee e = new Employee(); e.name = "SPLESSONS"; e.address = "Texas, US"; e.SSN = 123456; e.number = 111; try { FileOutputStream fileOut = new FileOutputStream("E:/SAI/employee.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(e); out.close(); fileOut.close(); System.out.printf("Serialized data is saved in given folder"); }catch(IOException i) { i.printStackTrace(); } } }[/java] As earlier discussed ObjectOutputStream is used to serialize an object, FileOutputStream is going to write the data to a file. Output When compile the code following is the result will be generated. [java]Serialized data is saved in given folder[/java] Now check the data in the folder, it will be as follows. [java]¬í sr serialization.Employee™…µ\—c I numberL addresst Ljava/lang/String;L nameq ~ xp ot Texas, USt SPLESSONS[/java]

Deserialization

shape Description

In Java Serialization -> deserialization is the process of rebuilding the current state of an object. Based on the above example now the following code is going to deserialize an object. DeserializeDemo.java [java]package serialization; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class DeserializeDemo { public static void main(String [] args) { Employee e = null; try { FileInputStream fileIn = new FileInputStream("E:/SAI/employee.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); e = (Employee) in.readObject(); in.close(); fileIn.close(); }catch(IOException i) { i.printStackTrace(); return; }catch(ClassNotFoundException c) { System.out.println("Employee class not found"); c.printStackTrace(); return; } System.out.println("Deserialized Employee, Object state is normal now"); System.out.println("Name: " + e.name); System.out.println("Address: " + e.address); System.out.println("SSN: " + e.SSN); System.out.println("Number: " + e.number); } } [/java] As discussed earlier ObjectInputStream is used to deserialized an object. FileInputStream is used to read the stream of characters. Output When compile the code result will be as follows. [java] Deserialized Employee, Object state is normal now Name: SPLESSONS Address: Texas, US SSN: 0 Number: 111 [/java]

Summary

shape Key Points

  • The println() is used to print a new line.
  • The printf()is used to format the string.