Java Reference
In-Depth Information
7.2 Text and Binary Files
A text file is a sequence of characters organized into lines. Conceptually, we think of each line as being terminated by
a newline character. However, depending on the host environment, certain character translations may occur.
For example, if we wrote the newline character \n to a file, it could be translated into two characters—a carriage return
and a line-feed character.
Thus, there is not necessarily a one-to-one correspondence between characters written and those stored on an
external device. Similarly, there may not be a one-to-one correspondence between the number of characters stored in
a file and the number read.
A binary file is simply a sequence of bytes, with no character translations occurring on input or output. Thus,
there is a one-to-one correspondence between what is read or written and what is stored in the file.
Apart from possible character translations, there are other differences between text and binary files. To illustrate,
a short integer is stored using 2 bytes (16 bits); the number 3371 is stored as 00001101 00101011 .
If we were to write this number to a text file, it would be written as the character 3, followed by the character 3,
followed by 7, followed by 1, occupying 4 bytes in all. On the other hand, we could simply write the two bytes as is to a
binary file.
Even though we could still think of them as a sequence of two “characters,” the values they contain may
not represent any valid characters. In fact, in this case, the decimal values of the two bytes are 13 and 43 , which,
interpreted as two ASCII characters, are the carriage return character (CR) and + .
Another way to look at it is that, in general, each byte in a text file contains a human-readable character, whereas,
in general, each byte in a binary file contains an arbitrary bit pattern. Binary files are important for writing data
directly from its internal representation to an external device, usually a disk file.
The standard input and output are considered text files. A disk file may be created as a text file or as a binary file.
We will see how to do so shortly.
7.3 Internal vs. External File Name
The usual way of using a computer is via its operating system. We normally create and edit files using a word
processor or a text file editor. When we create a file, we give it a name that we use whenever we need to do anything
with the file. This is the name by which the file is known to the operating system.
We will refer to such a name as an external file name. (The term external is used here to mean “external to a Java
program.”) When we are writing a program, we may want to specify, say, the reading of data from a file. The program
will need to use a file name, but, for several reasons, this name should not be an external file name. The following are
the major reasons:
The file to be read may not have been created as yet.
If the external name is tied to the program, the program will be able to read a file with that
name only. If the data is in a file with a different name, either the program will have to be
changed or the file renamed.
The program will be less portable since different operating systems have different file-naming
conventions. A valid external file name on one system may be invalid on another one.
For these reasons, a Java program uses an internal file name—we have generally used in for
input and out for output. For instance, when we write the following, we associate the internal
name, in , with the external file, input.txt :
Scanner in = new Scanner(new FileReader("input.txt"));
 
Search WWH ::




Custom Search