Java Reference
In-Depth Information
«interface»
java.io.DataOutput
OutputStream
+writeBoolean(b: boolean): void
+writeByte(v: int): void
Writes a Boolean to the output stream.
Writes the eight low-order bits of the argument
v
to
the output stream.
Writes the lower byte of the characters in a string to
the output stream.
Writes a character (composed of 2 bytes) to the
output stream.
Writes every character in the string
s
to the output
stream, in order, 2 bytes per character.
Writes a
float
value to the output stream.
Writes a
double
value to the output stream.
Writes an
int
value to the output stream.
Writes a
long
value to the output stream.
Writes a
short
value to the output stream.
Writes
s
string in UTF format.
FilterOutputStream
+writeBytes(s: String): void
+writeChar(c: char): void
DataOutputStream
+DataOutputStream
(out: OutputStream)
+writeChars(s: String): void
+writeFloat(v: float): void
+writeDouble(v: double): void
+writeInt(v: int): void
+writeLong(v: long): void
+writeShort(v: short): void
+writeUTF(s: String): void
F
IGURE
17.10
DataOutputStream
enables you to write primitive data-type values and strings into an output stream.
of ASCII characters, since an ASCII code is stored only in the lower byte of a Unicode. If a
string consists of non-ASCII characters, you have to use the
writeChars
method to write
the string.
The
writeUTF(String s)
method writes two bytes of length information to the
output stream, followed by the modified UTF-8 representation of every character in the
string
s
. UTF-8 is a coding scheme that allows systems to operate with both ASCII and
Unicode. Most operating systems use ASCII. Java uses Unicode. The ASCII character set
is a subset of the Unicode character set. Since most applications need only the ASCII char-
acter set, it is a waste to represent an 8-bit ASCII character as a 16-bit Unicode character.
The modified UTF-8 scheme stores a character using one, two, or three bytes. Characters
are coded in one byte if their code is less than or equal to
0x7F
, in two bytes if their code is
greater than
0x7F
and less than or equal to
0x7FF
, or in three bytes if their code is greater
than
0x7FF
.
The initial bits of a UTF-8 character indicate whether a character is stored in one byte, two
bytes, or three bytes. If the first bit is
0
, it is a one-byte character. If the first bits are
110
, it
is the first byte of a two-byte sequence. If the first bits are
1110
, it is the first byte of a three-
byte sequence. The information that indicates the number of characters in a string is stored
in the first two bytes preceding the UTF-8 characters. For example,
writeUTF("ABCDEF")
actually writes eight bytes (i.e.,
00 06 41 42 43 44 45 46
) to the file, because the first
two bytes store the number of characters in the string.
The
writeUTF(String s)
method converts a string into a series of bytes in the UTF-8
format and writes them into an output stream. The
readUTF()
method reads a string that has
been written using the
writeUTF
method.
The UTF-8 format has the advantage of saving a byte for each ASCII character, because a
Unicode character takes up two bytes and an ASCII character in UTF-8 only one byte. If most
of the characters in a long string are regular ASCII characters, using UTF-8 is more efficient.
Creating
DataInputStream
/
DataOutputStream
DataInputStream
/
DataOutputStream
are created using the following constructors
(see Figures 17.9 and 17.10):
UTF-8 scheme
public
DataInputStream(InputStream instream)
public
DataOutputStream(OutputStream outstream)
Search WWH ::
Custom Search