/** File: Serialized.java * Copy: Copyright (c) 1998 by David G. Green * Vers: 1.0.0 11/10/98 dgg -- original example */ import java.io.*; class Serialized implements Serializable { private int id; private String name; /** Constructor which intializes the data items. */ Serialized( int an_id, String a_name ) { id = an_id; name = a_name; } /** Driver. Creates a stream and an object and serializes object to stream * Then reads stream recreating object */ public static void main( String [] args ) throws IOException, ClassNotFoundException { // create a stream to take serialized objects ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("test.dat")) ; // create an object to be serialized Serialized s = new Serialized( 20, "kermit" ); // write the object out to the file (stream) out.writeObject( s ); // toss the object s = null; // close file and reopen out.close(); ObjectInputStream in = new ObjectInputStream( new FileInputStream("test.dat" )); s = (Serialized) in.readObject(); System.out.println( "object id = " + s.id + " name = " + s.name ); in.close(); } }