public class AppendTextFile
1: //AppendTextFile.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 AppendTextFile
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 =
23: new PrintWriter(new FileOutputStream("out.txt", append));
24: }
25: catch(FileNotFoundException e)
26: {
27: System.out.println("Error opening the file out.txt.");
28: System.exit(0);
29: }
30: System.out.println("Enter three lines of text:");
31: Scanner keyboard = new Scanner(System.in);
32: for (int count = 1; count <= 3; count++)
33: {
34: String line = keyboard.nextLine();
35: outputStream.println(count + " " + line);
36: }
37: outputStream.close();
38: System.out.println("Those lines were written to out.txt.");
39: }
40: }
41: