Can a value from an int
variable
be written to a binary file without first translating it into characters?
Yes.
A Java int
uses patterns of 32 bits to represent integers.
The exact pattern contained in an int
can be written to four bytes
of a binary file.
OutputStream
is an abstract class from which all
byte-oriented output streams descend.
Its descendant classes are used for general-purpose
(non-character) output.
These streams
write groups of bytes to output destinations.
A FileOutputStream
connects an output stream of bytes
to a file.
It is concerned with the details of writing bytes to a file.
It is not concerned with how groups of bytes form larger units of data.
It is has methods for writing single bytes or an array of bytes to a file,
and some other methods.
These methods are not very useful for us since you have to deal with
the individual bytes of a data type.
A DataOuputStream
can be connected
to a FileOutputStream
to provide
methods that are convenient for writing various data types to a file.
A DataOuputStream
is concerned with how groups of bytes form larger units of data.
It has various methods for sending groups of bytes to a stream.
It is not concerned with the details of writing bytes to a file.
Use DataOuputStream
to send data of spectific types to a stream, and use
FileOutputStream
to send that stream to a disk file.
(See the example program on the next page.)
Does FileOutputStream
itself provide a way to write an int
to a disk?
Does DataOutputStream
provide a way to write an int
to a stream?