/* Simple Java serialization example for cs585 Demo program reads and writes a file of Items. Java's Serializable tutorial is at: http://java.sun.com/docs/books/tutorial/essential/io/providing.html Serialization must first write a file before it can read a file. To run demo java SerializableDemo [] example: java SerializableDemo filename false // create filename save items java SerializableDemo filename // open filename read it and write it Mike Barnes 9/19/03 */ import java.io.*; import java.util.*; // Vector public class SerializableDemo implements Serializable { Vector items; class Item implements Serializable { public String descript, note; public boolean hasNote; public Item() { hasNote = false; descript = note = null; } public Item(String description, String aNote) { hasNote = true; descript = description; note = aNote; } public Item(String description) { hasNote = false; descript = description; note = null; } public String toString() { String str = new String(descript + "\n" + hasNote + "\n" + note + "\n"); return str;} } // Item public SerializableDemo() { items = new Vector(); } // read all the items from open file file and place in vector items public void readItems(String fileName) throws IOException { ObjectInputStream fin = null; // file to read boolean done = false; Item item = null; // open file to read System.out.println("Reading"); try { fin = new ObjectInputStream( new FileInputStream(fileName)); } catch(IOException e) { System.out.println(fileName + " not found."); System.exit(0); } // read from file System.out.print("File " + fileName + " "); done = false; while(!done) { try { item = (Item) fin.readObject(); items.addElement(item); System.out.print(". "); // show progress in reading file } catch (EOFException eof) { done = true; System.out.println("at end of file, done"); System.out.println(); } catch (ClassNotFoundException cnf) { System.out.println("Item class not found, exit"); fin.close(); System.exit(0); } catch (IOException e) { System.out.println("Error reading items"); fin.close(); System.exit(0); } } } public void writeItems(String fileName) throws IOException { ObjectOutputStream fout = null; // file to write Item item; System.out.println("Writing"); try { fout = new ObjectOutputStream( new FileOutputStream(fileName)); } catch(IOException e) { System.out.println(fileName + " not found"); System.exit(0); } // write to file System.out.print("File " + fileName + " "); int count = items.size(); for(int i = 0; i < count; i++) { item = (Item) items.elementAt(i); fout.writeObject(item); fout.flush(); System.out.print(". "); } fout.close(); System.out.println(count + " items wrote"); } public void createItems() { items.addElement(new Item("listen to Pat Metheny")); items.addElement(new Item("585 p1", "Need to get a plan for how to complete the first project in 585. Perhaps I can look at some examples and then mock up some stuff.")); items.addElement(new Item("wash clothes")); items.addElement(new Item("write home")); items.addElement(new Item("organize hard disk", "need to have separate subdirectories for each version of my projects in each of the classes that I am taking.")); } public void showItems() { Enumeration enum; System.out.println("Show " + items.size() + " items read"); System.out.println(); enum = items.elements(); while (enum.hasMoreElements()) System.out.println( enum.nextElement()); } // open a file of items to read into a vector // show the vector of items read // write the vector of items to a file public static void main (String[] args) throws IOException { boolean done = false; String fileName = null; SerializableDemo demo = new SerializableDemo(); System.out.println(); switch (args.length) { case 1 : fileName = args[0]; demo.readItems(fileName); demo.showItems(); demo.writeItems(fileName); break; case 2 : fileName = args[0]; demo.createItems(); demo.showItems(); demo.writeItems(fileName); break; default : System.out.println("Usage:"); System.out.println("\t java SerializableDemo []"); System.out.println("\t examples:"); System.out.println("\t\tjava SerializableDemo filename"); System.out.println("\t\t\t // open filename read it and write it"); System.out.println("\t\t java SerializableDemo filename false"); System.out.println("\t\t\t // create filename and save items"); } } }