Page 639 - Introduction to Programming with Java: A Problem Solving Approach
P. 639

                filename might specify a directory rather than a file, (3) The filename might specify a nonexistent direc- tory followed by a filename (e.g., javaFiiles/Mouse.java). The FileNotFoundException is a checked exception, so the PrintWriter constructor call must be in a try block, and the corresponding catch block’s parameter must match the FileNotFoundException.
To open a file for text output, all you need to do is write the statement specified above. You don’t have to know how it works. But if you’re curious, here’s an explanation: Whenever you instantiate a PrintWriter object, you also automatically get a FileWriter object. That FileWriter object uses one of the write methods it inherits from its parent, OutputStreamWriter, to transform a stream (or sequence) of Uni- code characters into a stream of bytes—the file’s natural data format. In addition, FileWriter provides a buffer for that final stream of bytes. A buffer is a variable-length array that absorbs data-flow variations, so that the flow of data out of the processor (where the program is) does not have to be perfectly synchronized with the flow of data into memory (where the file is). Suppose the hardware that services memory is busy. If there were no buffer, the program would have to stop and wait until that hardware became available. But with a buffer, the processor can simply put the data into the buffer and let it pile up there. Then when the memory-servicing hardware becomes free, it can scoop up everything that has accumulated in the buffer and transfer it into memory in a relatively large chunk. Because of its ability to absorb variations in data flow rates, a buffer is a great performance enhancer—it makes a program run faster. So you can see that opening a file is a big operation. It builds a substantial “infrastructure” to carry data from the processor to the file whenever needed.
Example Program
See the WriteTextFile program in Figure 15.2. It illustrates how to use the PrintWriter class for text- Apago PDF Enhancer
file output. The PrintWriter constructor call causes the JVM to open the user-specified file as just de- scribed. If the specified file does not exist, the program creates a new one. If the specified file exists already, the program clears the contents of that file before writing the new data.
Because the PrintWriter constructor throws a checked exception, the WriteTextFile program embeds the PrintWriter constructor call in a try block, and it provides a corresponding
1
FileNotFoundException catch block. The catch block calls e.getMessage, which returns the
exception’s automatically generated error message.
To write to the opened text file, the program calls PrintWriter’s println method. As indi-
cated earlier, it works like System.out.println, which automatically appends a line terminator. The PrintWriter class also has print and printf methods, which work like System.out’s print and printf methods. These latter two methods do not supply line terminators automatically, so if you want new lines with them, be sure to supply explicit \n characters. All these methods accept String ar- guments and convert those strings into streams of characters. The FileWriter object in the background transforms the streams of characters into streams of bytes for the file.
To close the opened text file, the program calls PrintWriter’s close method using this format:
<PrintWriter-reference-variable>.close();
This flushes out any partially filled streams and disassembles the outputting “infrastructure.” Remember to
close a file after you’re done. If you write to a PrintWriter file and forget to close the file, all the data in
1 As indicated in the previous chapter, it’s possible to remove try and catch blocks from a method by adding a throws clause to the method header. But we recommend against this practice, because it unnecessarily separates exception handling from the point where the exception occurs, and that makes programs harder to understand and debug.
15.3 Text-FileOutput 605
 

















































































   637   638   639   640   641