Back to Top

Wednesday, 14 August 2013

JAVA: Deserializing an Object from File

 
      To deserialize an object from file, first get the serialized data from the file using FileInputStream. In case if you want to know how to serialize an object to file please check this article serialize an object to a file. Pass the FileInputStream object through ObjectInputStream. Now, get the object from ObjectInputStream using the API readObject().

Example:

Suppose we have a file named sampleClass.ser which contains the data of a serialized object of class SampleClass. Please check this post to create this file. Where SampleClass is a normal pojo class with 4 variables, among which 1 is declared as transient, that means, protected from serialization.




Class SampleClass :

package com.interview.question.java;

public class SampleClass implements java.io.Serializable{
     
  /**
    *
    * Sample serialized class.
    *
    * @author http://interviewquestionjava.blogspot.com
    *
    **/
     
      private static final long serialVersionUID = 1L;

      private String strName;
     
      private String strAddress;
     
      private String strUsername;
     
     /**
       *
       * we declared the variable strPassword as transient.
       * So, it will be shield from serialization.
       *
       **/
      private transient String strPassword;

      public String getStrName() {
            return strName;
      }

      public void setStrName(String strName) {
            this.strName = strName;
      }

      public String getStrAddress() {
            return strAddress;
      }

      public void setStrAddress(String strAddress) {
            this.strAddress = strAddress;
      }

      public String getStrUsername() {
            return strUsername;
      }

      public void setStrUsername(String strUsername) {
            this.strUsername = strUsername;
      }

      public String getStrPassword() {
            return strPassword;
      }

      public void setStrPassword(String strPassword) {
            this.strPassword = strPassword;
      }
     
     

}


 Now, during serialization of the object of this SampleClass, we set the following values. 

  objSampleClass.setStrName("Ashoka Maurya");
  objSampleClass.setStrAddress("Magadha");
  objSampleClass.setStrUsername("Chakravartin");
  objSampleClass.setStrPassword("Ashokavadana");


Now, we execute the DeserializeFileToObject class, to deserialize the object of SampleClass.

Class DeserializeFileToObject :


package com.interview.question.java;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class DeserializeFileToObject {

      /**
    *
    * This class deserializes data from a file
    * sampleClass.ser to an Object of SampleClass
    *
    * @author http://interviewquestionjava.blogspot.com
    *
    **/
     
      public static void main(String[] args) {
           
            SampleClass objSampleClass = null;
           
            try
            {
            FileInputStream objFileInputStream = new FileInputStream("src/com/interview/question/java/sampleClass.ser");
            ObjectInputStream objInputStream = new ObjectInputStream(objFileInputStream);
            objSampleClass = (SampleClass)objInputStream.readObject();
            objInputStream.close();
            objFileInputStream.close();
           
            System.out.println("======================= Deserialized Data =======================");
            System.out.println("strName : "+objSampleClass.getStrName());
            System.out.println("strAddress : "+objSampleClass.getStrAddress());
            System.out.println("strUsername : "+objSampleClass.getStrUsername());
            System.out.println("strPassword : "+objSampleClass.getStrPassword());
            System.out.println("=================================================================");
            }
            catch (Exception e)
            {                            
          e.printStackTrace();                 
            }

      }

}


Output :

======================= Deserialized Data =======================
strName : Ashoka Maurya
strAddress : Magadha
strUsername : Chakravartin
strPassword : null
=================================================================


Here, we can see, we get all the data from the deserialized object except strPassword, as it is decleared as transient.

4 Responses to “ JAVA: Deserializing an Object from File ”

Anonymous said...
14 August 2013 at 18:00

nice post


anij said...
14 August 2013 at 21:59

thanks buddy!
keep commenting!
:)


Naviya Nair said...
24 October 2016 at 14:58

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

Online Java Training Java 8 Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai Java Training Institutes

Java Training Institutes in Chennai Java Training in Chennai


Naviya Nair said...
24 October 2016 at 15:10

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

Online Java Training Java 8 Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai Java Training Institutes

Java Training Institutes in Chennai Java Training in Chennai


Post a Comment

Popular Posts

All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar
blog links Web Directory Academics Blogs