Java Serialized Writing.
Writing serialized java object to a file.
- java.io.ObjectOutputStream's writeObject() method serializes (marshals) an object.
- The marshalled object contains structural information necessary to reconstruct the object.
- You can build an java.io.ObjectOutputStream from any java.io.OutputStream.
-
Marshalling an object involves dissecting the object into its component elements,
which will be:
- If the object has already been written to this java.io.ObjectOutputStream, only a handle is written.
- An java.io.ObjectStreamClass identifying the class of the object is written first. (includes the name and serialVersionUID).
- Primitive fields will then be written to the stream.
- Any reference field (instances of other classes) are serialized recursively.
- Since reference data (object members) must also be sent, they must also implement Serializable.
Write an object - example:
First a class, Rectangle, that is made serializable:
package app;
import java.awt.Color;
public class Rectangle implements java.io.Serializable {
private int length;
private int width;
// This member variable will not be Serialized as it is transient.
transient private Color color;
public Rectangle () {}
public Rectangle(int width, int length, Color color) {
this.length=length;
this.width=width;
this.color=color;
}
@Override
public String toString() {
return " width: " + width + " length: " + length +" color: "+color;
}
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;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
Here is a main class to write the serializable object of a Rectangle to a file Rectangle.ser:
package app;
import java.awt.Color;
import java.io.*;
public class WriteObj {
public static void main(String args[]) {
Rectangle rect = new Rectangle(25, 60, Color.blue);
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream("Rectangle.ser"));
out.writeObject(rect);
System.out.println("Wrote Rectangle with \"" + rect + "\" to file");
}
catch (IOException e) {
System.err.println("Error writing object: " + e.getMessage());
}
finally {
try {
out.close();
}
catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
}
Here's console printout:
Wrote Rectangle with " width: 25 length: 60 color: java.awt.Color[r=0,g=0,b=255]" to 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.