To bring in information, a program opens a stream on an information source (a file, memory, a socket) and reads the information sequentially, as shown in Figure 86.
Figure 86. Reading information into a program.
Similarly, a program can send information to an external destination by opening a stream to a destination and writing the information out sequentially, as in Figure 87.
Figure 87. Writing information out of a program.
No matter where the data is coming from or going to and no matter what its type, the algorithms for sequentially reading and writing data are shown in Table 46.
Reading |
Writing |
---|---|
|
|
The java.io package contains a collection of stream classes that support these algorithms. To use these classes, a program needs to import the java.io package. The stream classes are divided into two class hierarchies, based on the data type (either characters or bytes) on which they operate, as show in Figure 88.
Figure 88. The java.io package contains two independent hierarchies of classes: one for reading and writing bytes and the other for reading and writing characters.
Character Streams
Reader [1] and Writer [2] are the abstract superclasses for character streams. Reader provides the API and partial implementation for readersstreams that read 16-bit charactersand Writer provides the API and partial implementation for writersstreams that write 16-bit characters. Subclasses of Reader and Writer implement specialized streams (Figure 89).
[1] http://java.sun.com/j2se/1.3/docs/api/java/io/Reader.html
[2] http://java.sun.com/j2se/1.3/docs/api/java/io/Writer.html
Figure 89. The class hierarchies for readers and writers in java.io. Subclasses of Reader and Writer implement specialized streams and are divided into two categories: those that read from or write to data sinks (shaded) and those that perform some sort of processing (unshaded).
Most programs should use readers and writers to read and write textual information. The reason is that they can handle any character in the Unicode character set, whereas the byte streams are limited to ISO-Latin-1 8-bit bytes.
Byte Streams
To read and write 8-bit bytes, programs should use the byte streams, descendants of InputStream [1] and OutputStream. [2] InputStream and OutputStream provide the API and partial implementation for input streams (streams that read 8-bit bytes) and output streams (streams that write 8-bit bytes) (Figure 90). These streams are typically used to read and write binary data, such as images and sounds. Two of the byte stream classes, ObjectInputStream and ObjectOutputStream, are used for object serialization. These classes are covered in the section Object Serialization (page 334).
[1] http://java.sun.com/j2se/1.3/docs/api/java/io/InputStream.html
[2] http://java.sun.com/j2se/1.3/docs/api/java/io/OutputStream.html
Figure 90. The class hierarchies for byte streams in java.io. Subclasses of InputStream and OutputStream are divided into two categories: data sink streams (shaded) and processing streams (unshaded).
Understanding the I/O Superclasses
Reader and InputStream define similar APIs but for different data types. For example, Reader contains these methods for reading characters and arrays of characters:
int read() int read(char cbuf[]) int read(char cbuf[], int offset, int length)
InputStream defines the same methods but for reading bytes and arrays of bytes:
int read() int read(byte cbuf[]) int read(byte cbuf[], int offset, int length)
Also, both Reader and InputStream provide methods for marking a location in the stream, skipping input, and resetting the current position.
Writer and OutputStream are similarly parallel. Writer defines these methods for writing characters and arrays of characters:
int write(int c) int write(char cbuf[]) int write(char cbuf[], int offset, int length)
And OutputStream defines the same methods for bytes:
int write(int c) int write(byte cbuf[]) int write(byte cbuf[], int offset, int length)
All the streamsreaders, writers, input streams, and output streamsare automatically opened when created. You should close any stream explicitly by calling its close method. Or, the garbage collector can implicitly close it, which occurs when the object is no longer referenced.
Security Consideration
Some I/O operations are subject to approval by the current security manager. The example programs in this chapter are standalone applications, which have no security manager. This code might not work in an applet, depending on the browser or the viewer in which it is running. See the online tutorial at http://java.sun.com/docs/books/tutorial/applet/practical/security.html for information about the security restrictions placed on applets.
Getting Started
Object-Oriented Programming Concepts
Language Basics
Object Basics and Simple Data Objects
Classes and Inheritance
Interfaces and Packages
Handling Errors Using Exceptions
Threads: Doing Two or More Tasks at Once
I/O: Reading and Writing
User Interfaces That Swing
Appendix A. Common Problems and Their Solutions
Appendix B. Internet-Ready Applets
Appendix C. Collections
Appendix D. Deprecated Thread Methods
Appendix E. Reference