Back to Top

Tuesday, 17 January 2012

JAVA: How to serialize an object to a file?


      By implementing an interface Serializable in the class whose instances are to be serialized.Then passing the instance to the ObjectOutputStream which is connected to a fileoutputstream. This will save the object to a file.

Example: 
   Now, we will see a practical implementation of serialization on an object and we will store the serialized data in a file.
Class SampleClass is a pojo class which implements java.io.Serializable . It contains four String fields, one of which is declared as transient. That means, we can't serialized this field.


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,  Class SerializeObjectToFile, is used to create an object of SampleClass with some values.
Then we serialized that object to a file name sampleClass.ser.

Class SerializeObjectToFile :


package com.interview.question.java;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class SerializeObjectToFile {

/**
    *
    * This class serialized an Object of SampleClass
    * to a file named sampleClass.ser
    *
    * @author http://interviewquestionjava.blogspot.com
    *
    **/

 public static void main(String[] args) {
 
  SampleClass objSampleClass = new SampleClass();
 
  objSampleClass.setStrName("Ashoka Maurya");
  objSampleClass.setStrAddress("Magadha");
  objSampleClass.setStrUsername("Chakravartin");
  objSampleClass.setStrPassword("Ashokavadana");
 
  try 
  {
   FileOutputStream objFileOutputStream = new FileOutputStream("src/com/interview/question/java/sampleClass.ser");
   ObjectOutputStream objOutputStream = new ObjectOutputStream(objFileOutputStream);
   objOutputStream.writeObject(objSampleClass);
   objOutputStream.close();
   objFileOutputStream.close();
  
   System.out.println("SampleClass is serialized successfully..");
  } 
  catch (IOException e) 
  {
   e.printStackTrace();
  }
 

 }

}


Now, to deserialize this data back to an object please check the post Deserializing an Object from File

3 Responses to “ JAVA: How to serialize an object to a file? ”

Anonymous said...
14 August 2013 at 10:48

awesome..


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

thanks buddy!
keep visiting!
:)


Venkatesh CS said...
29 May 2019 at 16:32

Thanks for sharing this post, really this is helpful for me.
Java Training in OMR


Post a Comment

Popular Posts

Subscribe via Email
Subscribe Java Interview Questions via Email
All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar