Objectives |
|
Data stored in a text file is represented in human-readable form. Data stored in a binary file is represented in binary form. You cannot read binary files. They are designed to be read by programs. For example, Java source programs are stored in text files and can be read by a text editor, but Java classes are stored in binary files and are read by the JVM. The advantage of binary files is that they are more efficient to process than text files.
Although it is not technically precise and correct, you can envision a text file as consisting of a sequence of characters and a binary file as consisting of a sequence of bits. For example, the decimal integer 199 is stored as the sequence of three characters, '1' , '9' , '9' , in a text file, and the same integer is stored as a byte -type value C7 in a binary file, because decimal 199 equals hex C7 (199 = 12 x 16 1 + 7).
Java offers many classes for performing file input and output. These classes can be categorized as text I/O classes and binary I/O classes . You learned how to read/write strings and numeric values from/to a text file using Scanner and PrintWriter in §8.8, "Text I/O." This section introduces the classes for performing binary I/O.