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