Java Serialized Reading.
Reading serialized java object from a file.
- java.io.ObjectInputStream's readObject() method deserializes (unmarshals) an object
- The marshalled object contains structural information necessary to reconstruct the object.
- You can build an java.io.ObjectInputStream from any java.io.InputStream.
- Beware of creating an java.io.ObjectInputStream from a stream which does not contain a serialized object, will result in a java.io.StreamCorruptedException.
- java.io.ObjectInputStream's readObject() method returns an java.lang.Object so you have to downcast the object to the appropriate class. (for the last you should catch the java.lang.ClassCastException)
-
Unmarshalling an object involves assemling the object from its component elements,
which will be:
- If the object has been read from the stream already, the reference to the existing object is returned
- First, the java.io.ObjectStreamClass is read ( includes verifying the serialVersion)
- The no-argument constructor of the first non-serializable superclass is called
-
Primitive data members are set directly from the stream
(No serializable class constructor is called and fields initializers are also ignored) - Any reference data members (instances of other classes) are deserialized recursively.
- Since reference data (object members) must also be sent, they must also implement Serializable.
Read an object - example:
First a class, Rectangle, that is made serializable (same as in previous session):
package app;
public class Rectangle implements java.io.Serializable {
private int length;
private int width;
public Rectangle () {}
public Rectangle(int width, int length) {
this.length=length;
this.width=width;
}
@Override
public String toString() {
return " width: " + width + " length: " + length;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
}
A main class to read the serializable object of a Rectangle from a file Rectangle.ser:
package app;
import java.io.*;
public class ReadObj {
public static void main(String args[]) {
ObjectInputStream in = null;
try {
in = new ObjectInputStream(
new FileInputStream("Rectangle.ser"));
Rectangle rect = (Rectangle) in.readObject();
System.out.println("Read Rectangle with \"" + rect + "\" from file");
}
catch (ClassCastException e) {
System.err.println("Error casting object to a Rectangle");
}
catch (ClassNotFoundException e) {
System.err.println("Class not found");
}
catch (IOException e) {
System.err.println("Error reading object: " + e.getMessage());
}
finally {
try {
in.close();
}
catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
}
Here's console printout:
Read Rectangle with " width: 25 length: 60 color: null" from file
Here is the hexadecimal representation of the Rectangle.ser file:
AC ED 00 05 73 72 00 0D 61 70 70 2E 52 65 63 74
61 6E 67 6C 65 87 2E CA DB 51 AA DE 4D 02 00 02
49 00 06 6C 65 6E 67 74 68 49 00 05 77 69 64 74
68 78 70 00 00 00 3C 00 00 00 19
© 2010 by Finnesand Data.
All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the
correctness in the pages or source codes.
The risk of using this web-site pages or any program
codes from this website is entirely at the individual user.