Java Reference
In-Depth Information
this method only for strings that contain characters between
\u0000
and
\u00ff
.
public abstract void
writeChars(String s)
tHRows IOException
Writes a
String
as a sequence of
char
. Each character is writ-
ten as two bytes with the high byte written first.
There are no
readBytes
or
readChars
methods to read the same number
of characters written by a
writeBytes
or
writeChars
invocation, therefore
you must use a loop on
readByte
or
readChar
to read strings written with
these methods. To do that you need a way to determine the length of
the string, perhaps by writing the length of the string first, or by using
an end-of-sequence character to mark its end. You could use
readFully
to read a full array of bytes if you wrote the length first, but that won't
work for
writeChars
because you want
char
values, not
byte
values.
20.6.2. The
Data
Stream Classes
For each
Data
interface there is a corresponding
Data
stream. In addition,
the
RandomAccessFile
class implements both the input and output
Data
in-
sion of its corresponding
Filter
class, so you can use
Data
streams to
filter other streams. Each
Data
class has constructors that take another
appropriate input or output stream. For example, you can use the fil-
tering to write data to a file by putting a
DataOutputStream
in front of a
FileOutputStream
object. You can then read the data by putting a
DataIn-
putStream
in front of a
FileInputStream
object:
public static void writeData(double[] data, String file)
throws IOException
{
OutputStream fout = new FileOutputStream(file);
DataOutputStream out = new DataOutputStream(fout);
out.writeInt(data.length);
for (double d : data)