|
|
Reading and Writing |
Character Streams
ReaderandWriterare the abstract superclasses for character streams injava.io.Readerprovides the API and partial implementation for readers--streams that read 16-bit characters--andWriterprovides the API and partial implementation for writers--streams that write 16-bit characters.Subclasses of
ReaderandWriterimplement specialized streams and are divided into two categories: those that read from or write to data sinks (shown in gray in the following figures) and those that perform some sort of processing (shown in white). The figure shows the class hierarchies for theReaderandWriterclasses.[PENDING: get two figures from book]
Most programs should use readers and writers to read and write information. This is because they both can handle any character in the Unicode character set (while the byte streams are limited to ISO-Latin-1 8-bit bytes).
Byte Streams
Programs should use the byte streams, descendants ofInputStreamandOutputStream, to read and write 8-bit bytes.InputStreamandOutputStreamprovide the API and some implementation for input streams (streams that read 8-bit bytes) and output streams (streams that write 8-bit bytes). These streams are typically used to read and write binary data such as images and sounds.As with
ReaderandWriter, subclasses ofInputStreamandOutputStreamprovide specialized I/O that falls into two categories: data sink streams and processing streams. Figure 56 shows the class hierarchies for the byte streams.[PENDING: get two figures from book]
As mentioned, two of the byte stream classes,
ObjectInputStreamandObjectOutputStream, are used for object serialization. These classes are fully covered in Object Serialization.Understanding the I/O Superclasses
ReaderandInputStreamdefine similar APIs but for different data types. For example,Readercontains these methods for reading characters and arrays of characters:int read() int read(char cbuf[]) int read(char cbuf[], int offset, int length)InputStreamdefines the same methods but for reading bytes and arrays of bytes:Also, bothint read() int read(byte cbuf[]) int read(byte cbuf[], int offset, int length)ReaderandInputStreamprovide methods for marking a location in the stream, skipping input, and resetting the current position.
WriterandOutputStreamare similarly parallel.Writerdefines these methods for writing characters and arrays of characters:Andint write(int c) int write(char cbuf[]) int write(char cbuf[], int offset, int length)OutputStreamdefines the same methods but for bytes:All of the streams--readers, writers, input streams, and output streams--are automatically opened when created. You can close any stream explicitly by calling itsint write(int c) int write(byte cbuf[]) int write(byte cbuf[], int offset, int length)closemethod. Or the garbage collector can implicitly close it, which occurs when the object is no longer referenced.Learn how to use a selected assortment of these two types of streams in the next two sections:
|
|
Reading and Writing |