Java Reference
In-Depth Information
able to recognize where the value begins and ends and then convert the token — the sequence of characters
that represents the value — to its binary form. Figure 8-2 illustrates this.
FIGURE 8-2
When you write strings to a stream as character data, by default the Unicode characters are automatically
converted to the local representation of the characters in the host machine, and these are then written to the
stream. When you read a string, the default mechanism converts the data from the stream back to Unicode
characters from the local machine representation. With character streams, your program reads and writes
Unicode characters, but the stream contains characters in the equivalent character encoding used by the local
computer.
You don't have to accept the default conversion process for character streams. Java allows named map-
pings between Unicode characters and sets of bytes to be defined, called charsets , and you can select an
available charset that is to apply when data is transferred to, or from, a particular character stream. This is
important when the data is being transferred to or from another computer where the data representation is
different from your PC. Without the correct mapping, your PC will not interpret data from the destination
machine correctly, and vice versa. I don't go into charsets in detail, but you can find more information on
defining and using charsets in the JDK documentation for the Charset class.
THE CLASSES FOR INPUT AND OUTPUT
There are quite a number of stream classes but, as you will see later, they form a reasonably logical structure.
After you see how they are related, you shouldn't have much trouble using them. I work through the class
hierarchy from the top down, so you can see how the classes hang together and how you can combine them
in different ways to suit different situations.
The package java.io contains two classes that provide the foundation for Java's support for stream I/O,
shown in Table 8-1 .
TABLE 8-1 : Package java.io Classes
 
 
 
 
Search WWH ::




Custom Search