Tuesday, 17 January 2012
JAVA: How to serialize an object to a file?
Do you like this Article?
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
Subscribe to:
Post Comments
(
Atom
)
Popular Posts
-
public - public means everyone can access it.That means it's in global scope. As, main method is called by JVM [ Java Virtual Machine...
-
throw is used to throw an exception in a program, explicitly . Whereas, throws is included in the method's declaration part, wi...
-
Singleton in one of the most popular yet controversial design pattern, in the world of object oriented programming. It's one of t...
-
Web Container / Servlet Container / Servlet Engine : In J2EE Architecture , a web container (also known as servlet container or ser...
-
Program compiles. But at runtime throws an error “NoSuchMethodError”.
-
Vector : It's synchronized. It's slower than ArrayList. It's generally used in ...
-
doGet(): protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, java.io.IOException – is a met...
-
In Java Programming Language , we must declare a variable name and type, before using it. The data type of a variable defines the th...
3 Responses to “ JAVA: How to serialize an object to a file? ”
14 August 2013 at 10:48
awesome..
14 August 2013 at 21:58
thanks buddy!
keep visiting!
:)
29 May 2019 at 16:32
Thanks for sharing this post, really this is helpful for me.
Java Training in OMR
Post a Comment