![]() ![]() ![]() ![]() |
Input and Output Streams |
The java.io package contains two classes, InputStream and OutputStream, from which most of the other classes in the package derive.The InputStream class is an abstract base class that provides a minimal programming interface and a partial implementation of input streams in Java. The InputStream class defines a methods for reading bytes or arrays of bytes, marking locations in the stream, skipping bytes of input, finding out the number of bytes that are available for reading, and resetting the current position within the stream. An input stream is automatically opened when you create it. You can explicitly close a stream with the
close()
method, or let it be closed implicitly when the object is garbage collected.The OutputStream class is an abstract base class that provides a minimal programming interface and a partial implementation of output streams in Java. OutputStream defines methods for writing bytes or arrays of bytes to the stream and flushing the stream. An output stream is automatically opened when you create it. You can explicitly close an output stream with the
close()
method, or let it be closed implicitly when the object is garbage collected.The java.io package contains several subclasses of InputStream and OutputStream that implement a specific input or output function. For example, FileInputStream and FileOutputStream are input and output streams that operate on a file on the native file system.
The first of the following two diagrams shows the class hierarchy for the input stream classes comprising the java.io package. InputStream inherits from the Object class; six classes inherit directly from InputStream. One of OutputStream's descendents, FilterInputStream, is itself an abstract class with four children. The second of the diagram shows the class hierarchy for the output stream classes contained in the java.io package. OutputStream inherits from the Object class; four classes inherit directly from OutputStream. One of OutputStream's descendents, FilterOutputStream, is itself an abstract class with three descendents.
Note: you can click on any of the class symbols in either diagram to visit the API documentation for that class.
Simple Input and Output Streams
The following is an overview of the input and output stream classes that inherit directly from InputStream and OutputStream that are not themselves abstract classes.Using Input and Output Streams later in this lesson, covers these streams.
- FileInputStream and FileOutputStream
- Used to read data from or write data to a file on the native file system.
- PipedInputStream and PipedOutputStream
- Implements the input and output components of a pipe. Pipes are used to channel the output from one program (or thread or code block) into the input of another. A PipedInputStream must be connected to a PipedOutputStream and vice versa.
- ByteArrayInputStream and ByteArrayOutputStream
- Reads data from or writes data to a byte array in memory.
- SequenceInputStream
- Concatenates multiple input streams into one input stream.
- StringBufferInputStream
- Allows programs to read from a StringBuffer as if it were an input stream.
Filtered Streams
FilterInputStream and FilterOutputStream are subclasses of InputStream and OutputStream, respectively, and are both themselves abstract classes These classes defines the interface for filtered streams. Filtered streams process the data as its being read or written. For example, the BufferedInputStream and BufferedOutputStream classes buffer the data while reading and writing to speed it up.The Working with Filtered Streams page later in this lesson show you how to use filtered streams and how to implement your own.
- DataInputStream and DataOutputStream
- Reads or writes primitive Java data types in a machine independent format.
- BufferedInputStream and BufferedOutputStream
- This is an efficient stream that buffers data while reading or writing.
- LineNumberInputStream
- An input stream that keeps track of line numbers while reading.
- PushbackInputStream
- An input stream with a one-byte pushback buffer.
- PrintStream
- An output stream with convenient printing methods.
And The Rest...
In addition to the streams classes, java.io contains these other classes:And interfaces:
- File
- Represents a file on the native file system.
- FileDescriptor
- [PENDING: What is this?]
- RandomAccessFile
- Represents a random access file.
- StreamTokenizer
- Breaks the contents of a stream into tokens. [PENDING: define tokens].
Using Random Access Files talks about how to use random access files. It also provides a special section that shows you how to write filters for objects that implement the DataInput and DataOutput interfaces. Filters implemented in this fashion are more flexible than regular filtered streams because they can be used on random access files and on some sequential files.
- DataInput and DataOutput
- These two interfaces describe streams that can read and write primitive Java types in machine independent format. DataInputStream, DataOutputStream and RandomAccessFile implement these interfaces.
- FilenameFilter
- [PENDING: what is this?]
![]() ![]() ![]() ![]() |
Input and Output Streams |