Java Reference
In-Depth Information
a letter (A), a number (7), or a symbol (&). A logical grouping of bytes that form
a piece of meaningful data (such as a last name or an ID number) is called a
field . A record is a group of related fields. For example, an employee record
would contain all the fields about an employee, such as name, date of birth,
address, and social security number. A file is a collection of related records
stored under a single name. A school's student file, for example, would consist of
thousands of individual student records. Each student record would contain the
same fields as the other records.
The java.io package contains a collection of file and stream classes that
support reading and writing data. Java is not concerned about the concept of a
record; therefore, it is the programmer's responsibility to organize and structure
the data to meet the requirements of the program.
Sequential versus Random Access Files
Java views each file as a sequential stream of bytes. Sequential means that
items are placed in a specific sequence — or in order — one after another. Java
reads and writes sequential files as streams of bytes from beginning to end.
Sequential files are used when data elements are manipulated in the order they
are stored. For example, a Java programmer might create a sequential file to
receive input data from a user or to back up a set of transactions in a batch. The
advantages of using sequential files include an increase in the speed at which the
program reads and writes data as well as contiguous storage of that data. Storing
data sequentially, however, can be tedious when frequent updates are required or
when specific records, often out of order, need retrieval. Think of a sequential
file as being like a cassette tape of recorded music: it plays from beginning to
end. Adding a new song in the middle involves recording the first half of the
original cassette tape on another cassette tape, then adding the song to the new
cassette tape. Finally, you would record the rest of the cassette tape to finish the
project.
If constant updating like inserting, deleting, and changing is involved, or if
direct access is needed, Java provides another file mechanism that does not
involve traversing the entire file. Random access files store data in noncontigu-
ous locations; their records can be retrieved in any order. Because they can locate
a particular record directly without reading all of the preceding records, random
access files also are called direct access files.
The java.io package, imported at the beginning of the BillPayer program,
provides classes that handle input and output for both sequential and random
access files. The requirement for the BillPayer program is to write data to a
sequential data file.
Opening a Connection to an External File
Reading data from or writing data to a data file is a three-step process:
(1) declare a variable to hold the stream of data; (2) construct an instance of the
stream and assign it to a file; and (3) read or write the data.
Search WWH ::




Custom Search