Java Reference
In-Depth Information
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 destina-
tion. One such object can be created from the BufferedWriter class, which represents an
efficient way to create text files.
15
The write() method is the simplest way to send information to the output stream's desti-
nation. For instance, a BufferedWriter write() 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 less-controlled fashion.
The procedure for using a filter on a stream is as follows:
1. Create a stream associated with a data source or a data destination.
2. Associate a filter with that stream.
3. 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 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 prac-
tice in the following sections.
Search WWH ::




Custom Search