Java The Complete Reference, Seventh Edition

(Greg DeLong) #1
1234567.123, -1234567.123);

}
}

The output is shown here:

Here are some numeric values in different formats.

Various integer formats: 3 (3) +3 00003

Default floating-point format: 1234567.123000
Floating-point with commas: 1,234,567.123000
Negative floating-point default: -1,234,567.123000
Negative floating-point option: (1,234,567.123000)

Line up positive and negative values:
1,234,567.12
-1,234,567.12

PrintStreamalso defines theformat( )method. It has these general forms:

PrintStream format(StringfmtString, Object ...args)

PrintStream format(Localeloc, StringfmtString, Object ...args)

It works exactly likeprintf( ).

DataOutputStream and DataInputStream

DataOutputStreamandDataInputStreamenable you to write or read primitive data to or
from a stream. They implement theDataOutputandDataInputinterfaces, respectively.
These interfaces define methods that convert primitive values to or from a sequence of
bytes. These streams make it easy to store binary data, such as integers or floating-point
values, in a file. Each is examined here.
DataOutputStreamextendsFilterOutputStream, which extendsOutputStream.
DataOutputStreamdefines the following constructor:

DataOutputStream(OutputStreamoutputStream)

Here,outputStreamspecifies the output stream to which data will be written.
DataOutputStreamsupports all of the methods defined by it superclasses. However,
it is the methods defined by theDataOutputinterface, which it implements, that make it
interesting.DataOutputdefines methods that convert values of a primitive type into a byte
sequence and then writes it to the underlying stream. Here is a sampling of these methods:

final void writeDouble(doublevalue) throws IOException
final void writeBoolean(booleanvalue) throws IOException
final void writeInt(intvalue) throws IOException

Here,valueis the value written to the stream.

576 Part II: The Java Library

Free download pdf