Deserializing an Object

In this article, we will learn about Deserializing an Object. The following DeserializeDemo program deserializes the employee object created in the SerializeDemo program.

Study the program and try to determine its output.

Example

import java.io.*;
public class Deserialize {

   public static void main(String [] args) {
      Employee e = null;
      try {
         FileInputStream fileIn = new FileInputStream("/tmp/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 the Employee");
      System.out.println("Name: " + e.name);
      System.out.println("Address: " + e.address);
      System.out.println("SSN: " + e.SSN);
      System.out.println("Number: " + e.number);
   }
}

Output

Deserialized the Employee
Name: Handy Opinion
Address: Handy Opinion, ABC
SSN: 12345678
Number:10

That’s all, we will learn about Deserializing an Object.

Next Article

1. Serialization in Java
2. Queue Interface In Java
3. List Interface in Java with Examples

Please share this post:
Posts created 75

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: