Source of AppendTextFile0.java


  1: //AppendTextFile0.java
  2: 
  3: import java.io.PrintWriter;
  4: //import java.io.FileOutputStream;
  5: import java.io.FileNotFoundException;
  6: import java.util.Scanner;
  7: 
  8: public class AppendTextFile0
  9: {
 10:     public static void main(String[] args)
 11:     {
 12: 
 13:                 System.out.println("Sending output to out.txt.");
 14:         PrintWriter outputStream = null;
 15:         try
 16:         {
 17:                  System.out.print("Enter Append to append to out.txt, "
 18:                 + "\nor New to create a new file: ");
 19:                          Scanner keyboard = new Scanner(System.in);
 20:                          String ans = keyboard.next();
 21:             boolean append = ans.equalsIgnoreCase("append");
 22:                         outputStream = new PrintWriter("out.txt", append);
 23:             //The above line does not compile, which is why we
 24:             //brought in the "intermediate" class FileOutputStream.
 25:         }
 26:         catch(FileNotFoundException e)
 27:         {
 28:             System.out.println("Error opening the file out.txt.");
 29:             System.exit(0);
 30:         }
 31:         System.out.println("Enter three lines of text:");
 32:         Scanner keyboard = new Scanner(System.in);
 33:         for (int count = 1; count <= 3; count++)
 34:         {
 35:             String line = keyboard.nextLine();
 36:             outputStream.println(count + " " + line);
 37:         }
 38:         outputStream.close();
 39:         System.out.println("Those lines were written to out.txt.");
 40:     }
 41: }
 42: