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:
    1. If the object has already been written to this java.io.ObjectOutputStream, only a handle is written.
    2. An java.io.ObjectStreamClass identifying the class of the object is written first. (includes the name and serialVersionUID).
    3. Primitive fields will then be written to the stream.
    4. Any reference field (instances of other classes) are serialized recursively.
    Static and transient fields will not be serialized.
  • 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());
      }
    }
  }
}
  • Note: Attempting to write a non-serializable object will not result in a compiler error, but in a java.io.NotSerializableException, which will be caught in the java.io.IOException catch.
    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
    You can download this example here (needed tools can be found in the right menu on this page).
  • Prev. in menu JavaSerialize/Writing.php Next in menu
    Prev. in menu JavaSerialize/Writing.php Next in menu
    © 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.