Section 11.9 Chapter Summary
Subsection 11.9.1 Technical Terms
absolute path name | binary file |
buffering | database |
data hierarchy | directory |
end-of-file character | field |
file | filtering |
input | object serialization |
output | path |
record | relative path name |
Unicode Text Format (UTF) |
Subsection 11.9.2 Summary of Important Points
- A file is a collection of data stored on a disk. A stream is an object that delivers data to and from other objects.
- An
InputStream
is a stream that delivers data to a program from an external source—such as the keyboard or a file.System.in
is an example of anInputStream
. AnOutputStream
is a stream that delivers data from a program to an external destination—such as the screen or a file.System.out
is an example of anOutputStream
. - Data can be viewed as a hierarchy. From highest to lowest, a database is a collection of files. A file is a collection of records. A record is a collection of fields. A field is a collection of bytes. A byte is a collection of 8 bits. A bit is one binary digit, either 0 or 1.
- A binary file is a sequence of 0s and 1s that is interpreted as a sequence of bytes. A text file is a sequence of 0s and 1s that is interpreted as a sequence of characters. A text file can be read by any text editor, but a binary file cannot.
InputStream
andOutputStream
are abstract classes that serve as the root classes for reading and writing binary data.Reader
andWriter
serve as root classes for text I/O. Buffering is a technique in which a buffer, a temporary region of memory, is used to store data while they are being input or output. - A text file contains a sequence of characters divided into lines by the
\n
character and ending with a special end-of-file character. - The standard algorithm for performing I/O on a file consists of three steps: (1) Open a stream to the file, (2) perform the I/O, and (3) close the stream.
- Designing effective I/O routines answers two questions: (1) What streams should I use to perform the I/O? (2) What methods should I use to do the reading or writing?
- To prevent damage to files when a program terminates abnormally, streams should be closed when they are no longer needed.
- Most I/O operations generate an
IOException
that should be caught in the I/O methods. - Text input uses a different technique to determine when the end of a file has been reached. Text input methods return
null
or-1
when they attempt to read the special end-of-file character. Binary files don’t contain an end-of-file character, so binary read methods throw anEOFException
when they attempt to read past the end of the file. - The
java.io.File
class provides methods that enable a program to interact with a file system. Its methods can be used to check a file’s attributes, including its name, directory, and path. - Streams can be joined together to perform I/O. For example, a
DataOutputStream
and aFileOutputStream
can be joined to perform output to a binary file. - A binary file is “raw” in the sense that it contains no markers within it that allow you to tell where one data element ends and another begins. The interpretation of binary data is up to the program that reads or writes the file.
- Object serialization is the process of writing an object to an output stream. Object deserialization is the reverse process of reading a serialized object from an input stream. These processes use the
java.io.\-ObjectOutputStream
andjava.io.ObjectInputStream
classes. - The
JFileChooser
class provides a dialog box that enables the user to select a file and directory when opening or saving a file.
Solutions 11.9.3 Solutions to Self-Study Exercises
11.3 CASE STUDY: Reading and Writing Text Files
11.3.4 Code Reuse: Designing Text File Output
Self-Study Exercise
11.3.5 Reading from a Text File
Self-Study Exercises
11.3.5.1. Read Loop.
11.3.6 Code Reuse: Designing Text File Input
Self-Study Exercise
11.3.6.1. Read Loop Characters.
11.4 The File
Class
11.4.2 Validating File Names
Self-Study Exercise
11.4.2.1. File Info.
11.5 Example: Reading and Writing Binary Files
11.5.2 Reading Binary Data
Self-Study Exercise
11.5.2.1. Find the error.
11.6 Object Serialization: Reading and Writing Objects
11.6.2 The ObjectIO
Class
Self-Study Exercise
11.6.2.1. Serializable?
You have attempted of activities on this page.