Java Reference
In-Depth Information
Method
addRecords
(lines 41-69) prompts the user to enter the various fields for each
record or the end-of-file key sequence when data entry is complete. Figure 15.4 lists the key
combinations for entering end-of-file for various computer systems.
Operating system
Key combination
UNIX/Linux/Mac OS X
<Enter> <Ctrl> d
Windows
<Ctrl> z
Fig. 15.4
|
End-of-file key combinations.
Lines 44-46 prompt the user for input. Line 48 uses
Scanner
method
hasNext
to
determine whether the end-of-file key combination has been entered. The loop executes
until
hasNext
encounters end-of-file.
Lines 53-54 use a
Scanner
to read data from the user, then output the data as a record
using the
Formatter
. Each
Scanner
input method throws a
NoSuchElementException
(handled in lines 61-65) if the data is in the wrong format (e.g., a
String
when an
int
is
expected) or if there's no more data to input. The record's information is output using
method
format
, which can perform identical formatting to the
System.out.printf
method used extensively in earlier chapters. Method
format
outputs a formatted
String
to the output destination of the
Formatter
object—the file
clients.txt
. The format
string
"%d%s%s%.2f%n"
indicates that the current record will be stored as an integer
(the account number) followed by a
String
(the first name), another
String
(the last
name) and a floating-point value (the balance). Each piece of information is separated
from the next by a space, and the double value (the balance) is output with two digits to
the right of the decimal point (as indicated by the
.2
in
%.2f
). The data in the text file can
be viewed with a text editor or retrieved later by a program designed to read the file
(Section 15.4.2).
When lines 66-68 execute, if the
Formatter
object is closed, a
FormatterClosedEx-
ception
will be thrown. This exception is handled in lines 76-80. [
Note:
You can also
output data to a text file using class
java.io.PrintWriter
, which provides
format
and
printf
methods for outputting formatted data.]
Lines 93-97 declare method
closeFile
, which closes the
Formatter
and the under-
lying output file. Line 96 closes the object by simply calling method
close
. If method
close
is not called explicitly, the operating system normally will close the file when pro-
gram execution terminates—this is an example of operating-system “housekeeping.”
However, you should always explicitly close a file when it's no longer needed.
Sample Output
The sample data for this application is shown in Fig. 15.5. In the sample output, the user
enters information for five accounts, then enters end-of-file to signal that data entry is
complete. The sample output does not show how the data records actually appear in the
file. In the next section, to verify that the file was created successfully, we present a pro-
gram that reads the file and prints its contents. Because this is a text file, you can also verify
the information simply by opening the file in a text editor.