Source of AppendTextFile.java


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