Serialization Example - Musings

By: Nicholas Duchon

Here is an example of serialization.


The classes are:

SerialExample (serialize an entire instance of this class)
      instance variables/fields:
   +--- vax type MyClassA
   +--- vay type MyClassA
   +--- vbm type MyClassB

MyClassA extends Object, implements Serializable
          toString
    +-- MyClassB extends MyClassA (thus is automatically serializable)
          toString (uses call to super, accessing MyClassA.toString stuff first

Code:

import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class SerialExample implements Serializable {
  MyClassA vax = new MyClassA ();
  MyClassA vay = new MyClassA ();
  MyClassB vbm = new MyClassB ();

  public static void main (String args []) {
    SerialExample se = new SerialExample ();
    writeMe ("data.ser", se);
    System.out.println ("---- before se:\n" + se);
    se = null;
    System.out.println ("\n---- nulled se:\n" + se);
    se = readMe ("data.ser");
    System.out.println ("\n---- readback se:\n" + se);
    
  } // end main
  
  static void writeMe (String fname, SerialExample ob) {
    FileOutputStream fos = null;
    ObjectOutputStream out = null;
    try {
      fos = new FileOutputStream (fname);
    } catch (FileNotFoundException e) {
      System.out.println ("FileOutputStream exception: " + e);
    } // end try/catch for new FileOutputStream
    
    try {
      out = new ObjectOutputStream (fos);
    } catch (IOException e) {
      System.out.println ("ObjectOutputStream exception: " + e);
    } // end try/catch new ObjectOutputStream
    

    try {    
      out.writeObject (ob);
      out.close ();
    } catch (IOException e) {
      System.out.println ("writeObject exception: " + e);
    } // end try writeObject
  } // end writeMe
  
  static SerialExample readMe (String fname) {
    FileInputStream fos = null;
    ObjectInputStream in = null;
    SerialExample ob = null;
    try {
      fos = new FileInputStream (fname);
    } catch (FileNotFoundException e) {
      System.out.println ("FileOutputStream exception: " + e);
    } // end try/catch for new FileOutputStream
    
    try {
      in = new ObjectInputStream (fos);
    } catch (IOException e) {
      System.out.println ("ObjectOutputStream exception: " + e);
    } // end try/catch new ObjectOutputStream
    

    try {    
      ob = (SerialExample) in.readObject ();
      in.close ();
    } catch (ClassNotFoundException e) {
      System.out.println ("readObject exception: " + e);
    } // end try writeObject
    catch (IOException e) {
      System.out.println ("readObject exception: " + e);
    } // end try writeObject
    return ob;
  } // end readMe
  
  public String toString () {
    return vax + "\n" + vay + "\n" + vbm;
  } // end toString
} // end class SerialExample

class MyClassA implements Serializable {
  int varA = 43223;
  double varB = 13.332;
  
  public String toString () {
    return "varA: " + varA + ", varB: " + varB;
  } // end toString
} // end MyClassA

class MyClassB extends MyClassA {
  String varC = "One interesting Day in the life";
  boolean varD = true;
  
  public String toString () {
    return super.toString () + ", varC: " + varC + ", varD: " + varD;
  } // end toString
} // end MyClassB


Console output:

 ----jGRASP exec: java SerialExample

---- before se:
varA: 43223, varB: 13.332
varA: 43223, varB: 13.332
varA: 43223, varB: 13.332, varC: One interesting Day in the life, varD: true

---- nulled se:
null

---- readback se:
varA: 43223, varB: 13.332
varA: 43223, varB: 13.332
varA: 43223, varB: 13.332, varC: One interesting Day in the life, varD: true

 ----jGRASP: operation complete.
++++++++++++++++++++++++++++++++++++++
Data file hex dump.

00000000  ac ed 00 05 73 72 00 0d  53 65 72 69 61 6c 45 78  |....sr..SerialEx|
00000010  61 6d 70 6c 65 df 40 3a  25 b3 b0 cf 9e 02 00 03  |ample.@:%.......|
00000020  4c 00 03 76 61 78 74 00  0a 4c 4d 79 43 6c 61 73  |L..vaxt..LMyClas|
00000030  73 41 3b 4c 00 03 76 61  79 71 00 7e 00 01 4c 00  |sA;L..vayq.~..L.|
00000040  03 76 62 6d 74 00 0a 4c  4d 79 43 6c 61 73 73 42  |.vbmt..LMyClassB|
00000050  3b 78 70 73 72 00 08 4d  79 43 6c 61 73 73 41 fc  |;xpsr..MyClassA.|
00000060  f2 4a 2a 21 09 28 e9 02  00 02 49 00 04 76 61 72  |.J*!.(....I..var|
00000070  41 44 00 04 76 61 72 42  78 70 00 00 a8 d7 40 2a  |AD..varBxp....@*|
00000080  a9 fb e7 6c 8b 44 73 71  00 7e 00 04 00 00 a8 d7  |...l.Dsq.~......|
00000090  40 2a a9 fb e7 6c 8b 44  73 72 00 08 4d 79 43 6c  |@*...l.Dsr..MyCl|
000000a0  61 73 73 42 76 a8 69 ab  4d 94 68 a2 02 00 02 5a  |assBv.i.M.h....Z|
000000b0  00 04 76 61 72 44 4c 00  04 76 61 72 43 74 00 12  |..varDL..varCt..|
000000c0  4c 6a 61 76 61 2f 6c 61  6e 67 2f 53 74 72 69 6e  |Ljava/lang/Strin|
000000d0  67 3b 78 71 00 7e 00 04  00 00 a8 d7 40 2a a9 fb  |g;xq.~......@*..|
000000e0  e7 6c 8b 44 01 74 00 1f  4f 6e 65 20 69 6e 74 65  |.l.D.t..One inte|
000000f0  72 65 73 74 69 6e 67 20  44 61 79 20 69 6e 20 74  |resting Day in t|
00000100  68 65 20 6c 69 66 65                              |he life|
++++++++++++++++++++++++++++++++++++++


ND.