import java.io.*; import java.util.*; import java.awt.*; public class Save3Objects { //------------------------------------------------------------------- // main() entry point //------------------------------------------------------------------- public static void main (String[] args) { Object anObject; FileOutputStream file; ObjectOutputStream freezer = null; // create serialization stream "sermech.ser" try { file = new FileOutputStream("sermech.ser"); freezer = new ObjectOutputStream(file); } catch (IOException noIO) { System.out.println("IO error occurred: " + noIO); System.exit(10); } // create first test object, type Point, and serialize it. anObject = new Point(10,20); serializeObject(anObject, freezer); // create second test object, of different type Date, and serialize it. anObject = new Date(); serializeObject(anObject, freezer); // create third test object, type Point again, and serialize it. anObject = new Point(255,254); serializeObject(anObject, freezer); // terminate serialization stream. try { freezer.close(); } catch (IOException notClosed) { System.out.println("Could not close serialization stream !"); } } //------------------------------------------------------------------- // utility function to serialize a single object (hiding try-catch) //------------------------------------------------------------------- protected static void serializeObject(Object x, ObjectOutputStream serStream) { try { serStream.writeObject( x ); } catch (IOException noIO) { System.out.println("Serialization IO error occurred: " + noIO); System.exit(100); } } } // End of Class Save3Objects