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..


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

thanks buddy!
keep visiting!
:)


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

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

Subscribe via Email
Subscribe Java Interview Questions via Email
All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar
blog links Web Directory Academics Blogs