Also available as SerializeArgs.java

/**
 * Title: SerializeArgs
 * Description: Using Serializable interface and ObjectStreams
 *   to read and write args to a file.
 *
 * @author hollingd@cs.rpi.edu
 */

import java.io.*;


// save argv in a file as an array of Strings

public class SerializeArgs {

  // The name of the file we read/write
  static final String fileName = "saveargs";

  static public void main(String []args) {
        // first read whatever was last saved in the file
        read();
        // now save whatever we got in args
        write(args);

  }

  /** read will read from the file, treating it as an
   *  objectstream. The assumption is that the file contains
   *  a copy of argv from the last time the program was run...
   */
  static void read() {
        String []blah;
        try {
                ObjectInputStream ois = new ObjectInputStream(
                                                                  new FileInputStream(fileName));

                blah = (String[]) ois.readObject();

                // print out what we got
                System.out.println("Here is what we found in " + fileName);
                for (String s : blah) {
                  System.out.println(s);
                }
        } catch (IOException ioe) {
          System.out.println("Reading failed  - no file ?");
        } catch (ClassNotFoundException cnfe) {
          System.out.println("Something is wrong with the file....");
        }
  }

  /** write will serialize the arrary of strings and write
   *  to the file. 
   */
  static void write(String[] args) {
        try {
          ObjectOutputStream dos = new ObjectOutputStream(
                                                                new FileOutputStream(fileName));
          dos.writeObject(args);
          dos.close();
        } catch (IOException ioe) {
          System.out.println("Error writing to " + fileName);
        }
  }
}