Most classes that work directly with streams are part of the
java.io package. There are also a few streams classes in
java.util.zip
.
A stream is a sequence of data.
The Java I/O stream is an abstraction of a data source or a data destination and represents an object that can produce data or receive data.
An input stream is used to read data from a data source and an output stream is used to write data to a data destination.
Just as a stream of water represents a continuous flow of water, a Java stream produces or consumes a continuous flow of data.
I/O streams are used to move data from a data source to a Java program, and from a Java program to a data destination.
- An input stream enables you to read data from a data source to a Java application.
- An output stream enables you to write data from a Java application to a data destination.
The
java.util.zip
package contains four input stream classes that read data in a compressed format and return it in
uncompressed format and four output stream classes that read data in uncompressed format and write in compressed format.
The two main classes are
- java.io.InputStream and
- java.io.OutputStream
Abstract base Classes
These are abstract base classes for
many different subclasses with more-specialized abilities.
Although
InputStream and
OutputStream are abstract classes, many methods in the class library are only
specified to return an
InputStream or
OutputStream, the not
more-specific subclass.
Although
InputStream and
OutputStream are abstract classes, many methods in the class library are only specified to return an
InputStream or OutputStream, not the more-specific subclass.
For example, this is the signature for the
openStream() method in
java.net.URL
:
public final InputStream openStream()
throws IOException
Channels serve as conduits to I/O services. I/O falls into two broad categories:
- file I/O and
- stream I/O.
It is no surprise that there are two types of channels: 1) file and 2) socket.
There is one FileChannel class and three socket channel classes:
- SocketChannel,
- ServerSocketChannel, and
- DatagramChannel.
Channels can be created in several ways. The socket channels have factory methods to create new socket channels directly. But a FileChannel object can be obtained only by calling the getChannel() method on an open RandomAccessFile, FileInputStream, or FileOutputStream object.
You cannot create a FileChannel object directly. File and socket channels are discussed detail in upcoming sections.
SocketChannel sc = SocketChannel.open();
sc.connect (new InetSocketAddress ("somehost", someport));
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind (new InetSocketAddress (somelocalport));
DatagramChannel dc = DatagramChannel.open();
RandomAccessFile raf = new RandomAccessFile ("somefile", "r");
FileChannel fc = raf.getChannel();
The socket classes of java.net have new getChannel() methods as well. While these methods return a corresponding socket channel object, they are not sources of new channels as RandomAccessFile.getChannel() is.
They return the channel associated with a socket if one already exists; they never create new channels.
The ByteArrayOutputStream class writes data into the successive components of a byte array using the methods of java.io.OutputStream :
public class ByteArrayOutputStream extends OutputStream
java.io.InputStream
Stream Basics Quiz
Click the Quiz link below to take a brief multiple-choice quiz on the basics of streams.
Stream Basics Quiz