Writing Formatted Values
Although formatting of values is covered extensively in Chapter 18, p. 1095, here we mention the support for formatting values provided by I/O streams. The PrintWriter class provides the format() methods and the printf() convenient methods to write formatted values. The printf() methods are functionally equivalent to the format() methods. As the methods return a PrintWriter, calls to these methods can be chained.
The printf() and the format() methods for printing formatted values are also provided by the PrintStream and the Console classes (p. 1256). The format() method is also provided by the String class (§8.4, p. 457). We assume familiarity with printing formatted values on the standard output stream by calling the printf() method on the System.out field which is an object of the PrintStream class (§1.9, p. 24).
PrintWriter format(String format, Object… args)
PrintWriter format(Locale loc, String format, Object… args)
PrintWriter printf(String format, Object… args)
PrintWriter printf(Locale loc, String format, Object… args)
The String parameter format specifies how formatting will be done. It contains format specifiers that determine how each subsequent value in the variable arity parameter args will be formatted and printed. The resulting string from the formatting will be written to the current writer.
If the locale is specified, it is taken into consideration to format the args.
Any error in the format string will result in a runtime exception.
Writing Text Files
When writing text representation of values to a file using the default character encoding, any one of the following four procedures for setting up a PrintWriter can be used.
Setting up a PrintWriter based on an OutputStreamWriter which is chained to a FileOutputStream (Figure 20.4(a)):
Figure 20.4 Setting Up a PrintWriter to Write to a File
Create a FileOutputStream:
FileOutputStream outputFile = new FileOutputStream(“info.txt”);
Create an OutputStreamWriter which is chained to the FileOutputStream:
OutputStreamWriter outputStream = new OutputStreamWriter(outputFile);
The OutputStreamWriter uses the default character encoding for writing the characters to the file.
Create a PrintWriter which is chained to the OutputStreamWriter:
PrintWriter printWriter1 = new PrintWriter(outputStream, true);
The value true for the second parameter in the constructor will result in the output buffer being flushed by the println() and printf() methods.
Setting up a PrintWriter based on a FileOutputStream (Figure 20.4(b)):
Create a FileOutputStream:
FileOutputStream outputFile = new FileOutputStream(“info.txt”);
Create a PrintWriter which is chained to the FileOutputStream:
PrintWriter printWriter2 = new PrintWriter(outputFile, true);
The intermediate OutputStreamWriter to convert the characters using the default encoding is automatically supplied. The output buffer will also perform automatic line flushing.
Setting up a PrintWriter based on a FileWriter (Figure 20.4(c)):
Create a FileWriter which is a subclass of OutputStreamWriter:
FileWriter fileWriter = new FileWriter(“info.txt”);
This is equivalent to having an OutputStreamWriter chained to a FileOutputStream for writing the characters to the file, as shown in Figure 20.4(a).
Create a PrintWriter which is chained to the FileWriter:
PrintWriter printWriter3 = new PrintWriter(fileWriter, true);
The output buffer will be flushed by the println() and printf() methods.
Setting up a PrintWriter, given the file name (Figure 20.4(d)):
Create a PrintWriter, supplying the file name:
PrintWriter printWriter4 = new PrintWriter(“info.txt”);
The underlying OutputStreamWriter is created to write the characters to the file in the default encoding, as shown in Figure 20.4(d). In this case, there is no automatic flushing by the println() and printf() methods.
If a specific character encoding is desired for the writer, the third procedure (Figure 20.4(c)) can be used, with the encoding being specified for the FileWriter:
Charset utf8 = Charset.forName(“UTF-8”);
FileWriter fileWriter = new FileWriter(“info.txt”, utf8);
PrintWriter printWriter5 = new PrintWriter(fileWriter, true);
This writer will use the UTF-8 character encoding to write the characters to the file. Alternatively, we can use a PrintWriter constructor that accepts a character encoding:
Charset utf8 = Charset.forName(“UTF-8”);
PrintWriter printWriter6 = new PrintWriter(“info.txt”, utf8);
A BufferedWriter can also be used to improve the efficiency of writing characters to the underlying stream, as explained later in this section.