Page 638 - Introduction to Programming with Java: A Problem Solving Approach
P. 638
604 Chapter 15 Files
argument. We present text-file examples that use PrintWriter, Scanner, and FileReader in Sections 15.3 and 15.4.
The most efficient way to store homogeneous arrays of primitive data is in binary files. To write primi- tive data to a binary file, you instantiate an object of the DataOutputStream class with an argument that refers to an object of the FileOutputStream class. (A stream is a sequential flow of data.) To read primitive data from a binary file, you instantiate an object of the DataInputStream class with an argu- ment that refers to an object of the FileInputStream class. We illustrate use of DataOutputStream, FileOutputStream, DataInputStream, and FileInputStream in Section 15.7.
If you have a substantial amount of data in object form that you need to transfer to other Java pro- grams, you’ll want to use object files. To write objects (and any combination of primitives) to an object file, you instantiate an object of the ObjectOutputStream class with an argument that refers to an object of the FileOutputStream class. To read objects from an object file, you instantiate an object of the ObjectInputStream class with an argument that refers to an object of the FileInputStream class. We present examples that use ObjectOutputStream, FileOutputStream, ObjectInputStream, and FileInputStream in Section 15.8.
If you’re interested in the alternative Java API file I/O classes, see Sun’s Java API Web site at http://java .sun.com/javase/6/docs/api/.
15.3 Text-File Output
Readers who want to use file I/O early have the option of reading this section and the next section after
completing Chapter 3, Section 3.23. If you elect to jump from Chapter 3 to here, you should be aware that
Apago PDF Enhancer
some of the material in Sections 15.3 and 15.4 won’t make sense. But if you treat Figure 15.2’s program as a recipe, it will show you how to output to a file anything you can output to the computer screen. Likewise, if you treat Figure 15.5’s program as a recipe, it will show you how to input from a file anything you can input from the keyboard.
In this section, we show you how to use a PrintWriter object to output text to a file. In Section 15.4, we show you how to use a Scanner object to input text from a file. For all file I/O, there are three basic steps:
• Open the file by instantiating the appropriate class(es).
• Write to or read from the file by calling the appropriate method. • Close the file by calling the close method.
Let’s now consider these three steps in relation to the PrintWriter class. Opening a Text File for Output
To open a text file for output, instantiate the PrintWriter class like this:
PrintWriter <reference-variable>;
...
<reference-variable> = new PrintWriter(<filename>);
Note that there is no explicit “open” command in this statement. You just instantiate a PrintWriter ob- ject and that automatically opens the file specified by the PrintWriter constructor’s filename argument. If the filename is invalid, the JVM throws a FileNotFoundException. There are several ways for the filename to be invalid: (1) The filename might contain an invalid filename character, like an asterisk, (2) The