public class AppendTextFile
1: //AppendTextFile.java
2:
3: import java.io.FileNotFoundException;
4: import java.io.FileOutputStream;
5: import java.io.PrintWriter;
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:
15: PrintWriter outputStream = null;
16: try
17: {
18: System.out.println("Enter Append for append or "
19: + "New for a new file:");
20: Scanner keyboard = new Scanner(System.in);
21: String ans = keyboard.next();
22: boolean append = ans.equalsIgnoreCase("append");
23: //true if append, false if new
24:
25: outputStream =
26: new PrintWriter(new FileOutputStream("out.txt", append));
27: }
28: catch (FileNotFoundException e)
29: {
30: System.out.println("Error opening the file out.txt.");
31: System.exit(0);
32: }
33:
34: System.out.println("Enter three lines of text:");
35: String line = null;
36: Scanner keyboard = new Scanner(System.in);
37: for (int count = 1; count <= 3; count++)
38: {
39: line = keyboard.nextLine();
40: outputStream.println(count + " " + line);
41: }
42: outputStream.close();
43: System.out.println("Those lines were written to out.txt.");
44: }
45: }