Java Reference
In-Depth Information
Section 15.5 Object Serialization
• Java provides a mechanism called object serialization (p. 662) that enables entire objects to be
written to or read from a stream.
• A serialized object (p. 662) is represented as a sequence of bytes that includes the object's data as
well as information about the object's type and the types of data it stores.
• After a serialized object has been written into a file, it can be read from the file and deserialized
(p. 662) to recreate the object in memory.
• Classes
ObjectInputStream
(p. 662) and
ObjectOutputStream
(p. 662) enable entire objects to
be read from or written to a stream (possibly a file).
• Only classes that implement interface
Serializable
(p. 663) can be serialized and deserialized.
•The
ObjectOutput
interface (p. 662) contains method
writeObject
(p. 663), which takes an
Ob-
ject
as an argument and writes its information to an
OutputStream
. A class that implements this
interface, such as
ObjectOutputStream
, would ensure that the
Object
is
Serializable
.
•The
ObjectInput
interface (p. 662) contains method
readObject
(p. 663), which reads and re-
turns a reference to an
Object
from an
InputStream
. After an object has been read, its reference
can be cast to the object's actual type.
Section 15.6 Opening Files with
JFileChooser
• Class
JFileChooser
(p. 670) is used to display a dialog that enables users of a program to easily
select files or directories from a GUI.
Section 15.7 (Optional) Additional
java.io
Classes
•
InputStream
and
OutputStream
are
abstract
classes for performing byte-based I/O.
• Pipes (p. 673) are synchronized communication channels between threads. One thread sends
data via a
PipedOutputStream
(p. 673). The target thread reads information from the pipe via a
PipedInputStream
(p. 673).
• A filter stream (p. 674) provides additional functionality, such as aggregating data bytes into
meaningful primitive-type units.
FilterInputStream
(p. 674) and
FilterOutputStream
are typ-
ically extended, so some of their filtering capabilities are provided by their concrete subclasses.
•A
PrintStream
(p. 674) performs text output.
System.out
and
System.err
are
PrintStream
s.
• Interface
DataInput
describes methods for reading primitive types from an input stream. Classes
DataInputStream
(p. 674) and
RandomAccessFile
each implement this interface.
• Interface
DataOutput
describes methods for writing primitive types to an output stream. Classes
DataOutputStream
(p. 674) and
RandomAccessFile
each implement this interface.
• Buffering is an I/O-performance-enhancement technique. Buffering reduces the number of I/O
operations by combining smaller outputs together in memory. The number of physical I/O op-
erations is much smaller than the number of I/O requests issued by the program.
•With a
BufferedOutputStream
(p. 674) each output operation is directed to a buffer (p. 674) large
enough to hold the data of many output operations. Transfer to the output device is performed in
one large physical output operation (p. 674) when the buffer fills. A partially filled buffer can be
forced out to the device at any time by invoking the stream object's
flush
method (p. 674).
•With a
BufferedInputStream
(p. 675), many “logical” chunks of data from a file are read as one
large physical input operation (p. 675) into a memory buffer. As a program requests data, it's taken
from the buffer. When the buffer is empty, the next actual physical input operation is performed.
•A
ByteArrayInputStream
reads from a
byte
array in memory. A
ByteArrayOutputStream
outputs
to a
byte
array in memory.