Java Reference
In-Depth Information
Other types of information can be read from an object input stream with the following
methods:
read()
—Reads the next byte from the stream, which is returned as an
int
.
n
read(
byte[]
,
int
,
int
)
—Reads bytes into the specified byte array. The second
argument specifies the first array element where a byte should be stored. The last
argument represents the number of subsequent elements to read and store in the
array.
n
readBoolean()
—Reads a
boolean
value from the stream.
n
readByte()
—Reads a
byte
value from the stream.
n
readChar()
—Reads a
char
value from the stream.
n
readDouble()
—Reads a
double
value from the stream.
n
readFloat()
—Reads a
float
value from the stream.
n
readInt()
—Reads an
int
value from the stream.
n
readLine()
—Reads a
String
from the stream.
n
readLong()
—Reads a
long
value from the stream.
n
readShort()
—Reads a
short
value from the stream.
n
readUnsignedByte()
—Reads an unsigned byte value and returns it as an
int
.
n
readUnsignedShort()
—Reads an unsigned short value and returns it as an
int
.
n
Each of these methods throws an
IOException
if an input/output error occurs as the
stream is being read.
When an object is created by reading an object stream, it is created entirely from the
variable and object information stored in that stream. No constructor method is called to
create variables and set them up with initial values. There's no difference between this
object and the one originally serialized.
Listing 16.2 contains a Java application that reads an object from a stream and displays
its variables to standard output. The
ObjectReader
application loads the object serialized
to the file
message.obj
.
This class must be run from the same folder that contains the file
message.obj
and the
Message
class.