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.

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

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

nice post


Anijit Sarkar said...
14 August 2013 at 21:59

thanks buddy!
keep commenting!
:)


Government Jobs in Himachal Pradesh said...
9 June 2022 at 17:21

The information you have produced is so good and helpful, I will visit your website regularly.

bsc 1st year result 2021


Post a Comment

Popular Posts

All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar