Wednesday, 14 August 2013
JAVA: Deserializing an Object from File
Do you like this Article?
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();
}
}
}
======================= 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.
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: Deserializing an Object from File ”
14 August 2013 at 18:00
nice post
14 August 2013 at 21:59
thanks buddy!
keep commenting!
:)
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