//FileAppender.java

import java.io.*;

/**
 * A program that appends input to a file.
 * 
 * @author Zach Tomaszewski
 * @version 28 Sept 2004
 */
public class FileAppender {

  /**
   * Appends user input to the file named in the first command line
   * argument.
   *
   * @param args  Command line arguments
   */
  public static void main(String[] args) {
    if (args.length != 1) {
      System.out.println("\nFileAppender appends a line of user input " +
                         "to a given file.");
      System.out.println("\nUsage: java FileAppender {filename}\n");
    }else {
      try{
        FileAppender fa = new FileAppender();
        fa.appendInputToFile(args[0]);
      }catch (IOException ioe) {
        System.out.println("Could not open or read file: " + args[0]);
      }
    }
  }

  /**
   * Appends a single line of input from the user to the file named
   * in filename. The file must be a text (character) file.
   *
   * @param filename  The file to append to
   */
  public void appendInputToFile(String filename) throws IOException {
    BufferedReader stdin  = new BufferedReader(
                              new InputStreamReader(System.in));
    PrintWriter fileout = new PrintWriter(
                            new BufferedWriter(
                              new FileWriter(filename, true)));
    System.out.println("Enter a line of text to append to \"" + 
                       filename + "\": ");
    String userInput = stdin.readLine();
    fileout.println(userInput);
    fileout.close();
  }
}
