Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 138
نمايش فراداده

This abstract class is intended to act as a superclass for character output streams that filter the data written to them before writing it to some other character output stream. FilterWriter is declared abstract so that it cannot be instantiated. But none of its methods are themselves abstract: they all simply invoke the corresponding method on the output stream that was passed to the FilterWriter constructor. If you were allowed to instantiate a FilterWriter object, you'd find that it acts as a null filter (i.e., it simply passes the characters written to it along, without any filtration).

Because FilterWriter implements a null filter, it is an ideal superclass for classes that want to implement simple filters without having to override all of the methods of Writer. In order to create your own filtered character output stream, you should subclass FilterWriter and override all its write( ) methods to perform the desired filtering operation. Note that you can implement two of the write( ) methods in terms of the third and thus implement your filtering algorithm only once. In some cases, you may want to override other Writer methods and add other methods or constructors that are specific to your subclass. FilterWriter is the character-stream analog of FilterOutputStream.

Figure 9-24. java.io.FilterWriter

public abstract class 

FilterWriter extends Writer { // Protected Constructors protected

FilterWriter (Writer

out ); // Public Methods Overriding Writer public void

close ( ) throws IOException; public void

flush ( ) throws IOException; public void

write (int

c ) throws IOException; public void

write (char[ ]

cbuf , int

off , int

len ) throws IOException; public void

write (String

str , int

off , int

len ) throws IOException; // Protected Instance Fields protected Writer

out ; }