Java Reference
In-Depth Information
5. Line 19 chains a PrintWriter to the BufferedWriter .
6. Lines 20-22 write some HTML to the file.
7. Lines 23-25 close the streams.
Suppose we run the program with the following command line:
java com.sybex.io.FileDemo “This is a new file”
A fi le named test.html is created in the current working directory and the contents of
the fi le look like
<html><body><h1>
This is a new file
</h1></body></html>
You probably won't use this example in the real world to write a dynamic web page, but
the program does demonstrate how you can use the File class. It also demonstrates how
to chain a low-level writer ( FileWriter ) to a high-level writer ( BufferedWriter ), and even
how to chain a high-level writer ( BufferedWriter ) to another high-level writer ( Print-
Writer ) to achieve the buffered, fi ltered stream that Figure 4.8 illustrates.
FIGURE 4.8
Data written to the PrintWriter is buffered and eventually written to
the file.
PrintWriter
“<html>”
BufferedWriter
FileWriter
“test.html”
The following section demonstrates the concept of chaining streams together using byte
streams instead of character streams.
The FileInputStream and FileOutputStream Classes
The FileInputStream and FileOutputStream classes represent low-level streams that read and
write byte streams from fi les. As with FileReader and FileWriter , chain FileInputStream
Search WWH ::




Custom Search