This article, the first of three parts, will teach you how the programs you create in Java can interact with different storage devices using a communications system called streams. It is excerpted from chapter 15 of the book Sams Teach Yourself Java 2 in 21 days, written by Roger Cadenhead and Laura Lemay (Sams, ISBN: 0672326280).
Working with Input and Output in Java - Using a Stream (Page 2 of 5 )
The procedure for using either a byte stream or a character stream in Java is largely the same. Before you start working with the specifics of the java.io classes, it's useful to walk through the process of creating and using streams.
For an input stream, the first step is to create an object associated with the data source. For example, if the source is a file on your hard drive, a FileInputStream object could be associated with this file.
After you have a stream object, you can read information from that stream by using one of the object's methods. FileInputStream includes a read() method that returns a byte read from the file.
When you're finished reading information from the stream, you call the close() method to indicate that you're finished using the stream.
For an output stream, you begin by creating an object associated with the data's destination. One such object can be created from the BufferedWriter class, which represents an efficient way to create text files.
The write() method is the simplest way to send information to the output stream's destination. For instance, a BufferedWriterwrite() method can send individual characters to an output stream.
As with input streams, the close() method is called on an output stream when you have no more information to send.
Filtering a Stream
The simplest way to use a stream is to create it and then call its methods to send or receive data, depending on whether it's an output stream or an input stream.
Many of the classes you will work with today achieve more sophisticated results when a filter is associated with a stream before reading or writing any data.
A filter is a type of stream that modifies the way an existing stream is handled. Think of a dam on a mountain stream. The dam regulates the flow of water from the points upstream to the points downstream. The dam is a type of filteróremove it, and the water would flow in a much less controlled fashion.
The procedure for using a filter on a stream is as follows:
Create a stream associated with a data source or a data destination.
Associate a filter with that stream.
Read or write data from the filter rather than the original stream.
The methods you call on a filter are the same as the methods you would call on a stream. There are read() and write() methods, just as there would be on an unfiltered stream.
You can even associate a filter with another filter, so the following path for information is possible: an input stream associated with a text file, which is filtered through a Spanish-to-English translation filter, which is then filtered through a no-profanity filter, and is finally sent to its destinationóa human being who wants to read it.
If this is confusing in the abstract, you will have opportunities to see the process in practice in the following sections.