Java Reference
In-Depth Information
Good Programming Practice 15.1
When building
String
s that represent path information, use
File.separator
to obtain
the local computer's proper separator character rather than explicitly using
/
or
\
. This
constant is a
String
consisting of one character—the proper separator for the system.
Common Programming Error 15.1
Using
\
as a directory separator rather than
\\
in a string literal is a logic error. A single
\
indicates that the
\
followed by the next character represents an escape sequence. Use
\\
to insert a
\
in a string literal.
Next, we create and manipulate
sequential-access files
in which records are stored in order by
the record-key field. We begin with
text files
, enabling the reader to quickly create and edit
human-readable files. We discuss creating, writing data to, reading data from and updating
sequential-access text files. We also include a credit-inquiry program that retrieves data from
a file. The programs in Sections 15.4.1-15.4.3 are all in the chapter's
TextFileApps
direc-
tory so that they can manipulate the same text file, which is also stored in that directory.
Java imposes no structure on a file—notions such as records do not exist as part of the Java lan-
guage.
Therefore, you must structure files to meet the requirements of your applications.
In the following example, we see how to impose a
keyed
record structure on a file.
The program in this section creates a simple sequential-access file that might be used
in an accounts receivable system to keep track of the amounts owed to a company by its
credit clients. For each client, the program obtains from the user an account number and
the client's name and balance (i.e., the amount the client owes the company for goods and
services received). Each client's data constitutes a “record” for that client. This application
uses the account number as the
record key
—the file's records will be created and main-
tained in account-number order. The program assumes that the user enters the records in
account-number order. In a comprehensive accounts receivable system (based on sequen-
tial-access files), a
sorting
capability would be provided so that the user could enter the
records in
any
order. The records would then be sorted and written to the file.
Class
CreateTextFile
Class
CreateTextFile
(Fig. 15.3) uses a
Formatter
to output formatted
String
s, using
the same formatting capabilities as method
System.out.printf
. A
Formatter
object can
output to various locations, such as to a command window or to a file, as we do in this
example. The
Formatter
object is instantiated in line 26 in method
openFile
(lines 22-
38). The constructor used in line 26 takes one argument—a
String
containing the name
of the file, including its path. If a path is not specified, as is the case here, the JVM assumes
that the file is in the directory from which the program was executed. For text files, we use
the
.txt
file extension. If the file does
not
exist, it will be
created
. If an
existing
file is
opened, its contents are
truncated
—all the data in the file is
discarded
. If no exception oc-
curs, the file is open for writing and the resulting
Formatter
object can be used to write
data to the file.