We can use the FileWriter
stream class to write to a file text data that consists of both
strings and primitive type data. The following
PrintWriterFileApp
example gets the name for an output file either from a parameter
on the command line or from a default.
The program follows closely to PrintWriterApp
discussed in the Text Output to
Console section except that instead of starting with the PrintStream
object referenced by System.out,
we create a FileWriter
for a File
object and then wrap this with a BufferedWriter
and a PrintWriter.
These two wrappers give us the efficiency of stream buffering
and a handy set of print methods.