Java Reference
In-Depth Information
The method writeChar has one possibly surprising property: It expects its argument
to be of type int . So if you start with a value of type char , the char value can be type
cast to an int before it is given to the method writeChar . For example, to output the
contents of a char variable named symbol , you can use
writeChar
outputStream.writeChar(( int )symbol);
In actual fact, you do not need to write in the type cast to an int , because Java
automatically performs a type cast from a char value to an int value for you. So, the
following is equivalent to the previous invocation of writeChar :
outputStream.writeChar(symbol);
To output a value of type String , use the method writeUTF . For example, if
outputStream is a stream of type ObjectOutputStream , the following will write the
string "Hello friend." to the file connected to that stream:
writeUTF
for strings
outputStream.writeUTF("Hello friend.");
You may write output of different types to the same file. So, you may write a
combination of, for example, int , double , and String values. However, mixing types
in a file does require special care to make it possible to read them back out of the file.
To read them back, you need to know the order in which the various types appear in
the file, because, as you will see, a program that reads from the file will use a different
method to read data of each different type.
Note that, as illustrated in Display 10.13 and as you will see shortly, you close a
binary output or input stream in the same way that you close a stream connected to
a text file.
closing a
binary file
UTF and writeUTF
Recall that Java uses the Unicode character set, which is a set of characters that includes
many characters used in languages whose character sets are different from English.
Readers 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 programs. The ASCII character set is a subset of the Unicode character
set, so the Unicode 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
 
Search WWH ::




Custom Search