001package edu.pdx.cs410J.core;
002
003import java.io.*;
004
005/**
006 * This program writes the arguments from the command line to a text
007 * file.  It demonstrates the <code>FileWriter</code> class.
008 */
009public class WriteToFile {
010  private static PrintWriter err;
011
012  /**
013   * The first argument is the file to write to.
014   */
015  public static void main(String[] args) {
016    // Wrap a PrintWriter around System.err
017    err = new PrintWriter(System.err, true);
018
019    // Make a new FileWriter
020    Writer writer;
021    try {
022      writer = new FileWriter(args[0]);
023
024      // Write the command line arguments to the file
025      for (int i = 1; i < args.length; i++) {
026        writer.write(args[i]);
027        writer.write('\n');
028      }
029    
030      // All done
031      writer.flush();
032      writer.close();
033
034    } catch (IOException ex) {
035      err.println("** " + ex);
036    }
037  }
038}