Java Reference
In-Depth Information
Display 10.1 Sending Output to a Text File
1
import java.io.PrintWriter;
2
import java.io.FileOutputStream;
3
import java.io.FileNotFoundException;
4 public class TextFileOutputDemo
5{
6
public static void main(String[] args)
7
{
8
PrintWriter outputStream = null ;
9
try
10
{
11
outputStream =
12
new PrintWriter( new FileOutputStream("stuff.txt"));
13
}
14
catch (FileNotFoundException e)
15
{
System.out.println("Error opening the file stuff.txt.");
16
17
System.exit(0);
18
}
19
System.out.println("Writing to file.");
20
outputStream.println("The quick brown fox");
21
outputStream.println("jumped over the lazy dog.");
22
outputStream.close();
23
System.out.println("End of program.");
24
}
25
}
Sample Dialogue
Writing to file.
End of program.
F ILE File stuff.txt (after the program is run.)
The quick brown fox
jumped over the lazy dog.
You can read this file
using a text editor.
 
Search WWH ::




Custom Search