// WriteFile.java by Dr. I. for CS 301 during Summer, 1998
// This program demonstrates simple text file output

import java.io.*;

public class WriteFile  {
   public static void main(String[] args)  {
      String inFileName = "Garbage.in", outFileName = "Garbage.out", s;
      BufferedReader in = null;
      BufferedReader br = null;
      PrintWriter pw = null;
      
      // open input stream and input/output files
      //   Note:  assumes input stream exists, otherwise exit
      try  {
         in = new BufferedReader(new InputStreamReader(System.in));
         br = new BufferedReader(new FileReader(inFileName));
         pw = new PrintWriter(new FileWriter(outFileName), true);
      }
      catch (IOException e)  {
         System.err.println(e);
         System.exit(1);
      }
      
      // read from input stream and write to output file
      System.out.println("Read from input stream and write to output file:");
      System.out.println("Use ^Z to terminate input stream");
      try  {
         s = in.readLine();
         while (s != null)  {
            pw.println(s);
            s = in.readLine();
         }
         pw.close();
      }
      catch (IOException e)  {
         System.err.println(e);
         System.exit(1);
      }

      // read from input file and write to output stream
      System.out.println();  // blank line
      System.out.println("Read from input file and write to output stream:");
      try  {
         s = br.readLine();
         while (s != null)  {
            System.out.println(s);
            s = br.readLine();
         }
         br.close();
      }
      catch (IOException e)  {
         System.err.println(e);
         System.exit(1);
      }
   }
}

