Section 11.9 Chapter Summary
Subsection 11.9.1 Technical Terms
|absolute path name||binary file|
|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.
InputStreamis a stream that delivers data to a program from an external source—such as the keyboard or a file.
System.inis an example of an
OutputStreamis a stream that delivers data from a program to an external destination—such as the screen or a file.
System.outis an example of an
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.
OutputStreamare abstract classes that serve as the root classes for reading and writing binary data.
Writerserve 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
\ncharacter 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
IOExceptionthat 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
-1when 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 an
EOFExceptionwhen they attempt to read past the end of the file.
java.io.Fileclass 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
FileOutputStreamcan 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
JFileChooserclass provides a dialog box that enables the user to select a file and directory when opening or saving a file.