Java Reference
In-Depth Information
of this topic are undoubtedly using editors and operating systems that use the ASCII
character set, which is the character set normally used for English and for our Java pro-
grams. The ASCII character set is a subset of the Unicode character set, so the Uni-
code character set has a lot of characters you probably do not need. There is a standard
way of encoding all the Unicode characters, but for English-speaking countries, it is
not a very efficient coding scheme. The UTF coding scheme is an alternative scheme
that still codes all Unicode characters but that favors the ASCII character set. The
UTF coding method gives short, efficient codes for the ASCII characters. The price is
that it gives long, inefficient codes to the other Unicode characters. However, because
you probably do not use the other Unicode characters, this is a very favorable trade-
off. The method writeUTF uses the UTF coding method to write strings to a binary
file.
The method writeInt writes integers into a file using the same number of bytes—
that is, the same number of zeros and ones—to store any integer. Similarly, the method
writeLong uses the same number of bytes to store each value of type long . (But the
methods writeInt and writeLong use a different number of bytes from each other.) The
situation is the same for all the other write methods that write primitive types to binary
files. However, the method writeUTF uses differing numbers of bytes to store different
strings in a file. Longer strings require more bytes than shorter strings. This can present a
problem to Java, because there are no separators between data items in a binary file. The
way that Java manages to make this work is by writing some extra information at the
start of each string. This extra information tells how many bytes are used to write the
string, so readUTF knows how many bytes to read and convert. (The method readUTF
will be discussed a little later in this chapter, but, as you may have already guessed, it
reads a String value that was written using the UTF coding method.)
The situation with writeUTF is even a little more complicated than what we dis-
cussed in the previous paragraph. Notice that we said that the information at the start
of the string code in the file tells how many bytes to read, not how many characters are
in the string. These two figures are not the same. With the UTF way of encoding, dif-
ferent characters are encoded in different numbers of bytes. However, all the ASCII
characters are stored in just one byte, and you are undoubtedly using only ASCII char-
acters, so this difference is more theoretical than real to you now.
Reading Simple Data from a Binary File
The stream class ObjectInputStream is used to read from a file that has been written
to using ObjectOutputStream . Display 10.15 gives some of the most commonly used
methods for this class. If you compare that table with the methods for ObjectOutput-
Stream given in Display 10.14, you will see that each output method in Object-
OutputStream has a corresponding input method in ObjectInputStream . For example,
if you write an integer to a file using the method writeInt of ObjectOutputStream ,
then you can read that integer back with the method readInt of ObjectInputStream . If
you write a number to a file using the method writeDouble of ObjectOutputStream ,
Search WWH ::




Custom Search