Team LiB
Previous Section Next Section

Writerjava.io

Java 1.1appendable closeable flushable

This abstract class is the superclass of all character output streams. It is an analog to OutputStream, which is the superclass of all byte output streams. Writer defines the basic write( ) , flush( ), and close( ) methods all character output streams provide. The five versions of the write( ) method write a single character, a character array or subarray, or a string or substring to the destination of the stream. The most general version of this methodthe one that writes a specified portion of a character arrayis abstract and must be implemented by all subclasses. By default, the other write( ) methods are implemented in terms of this abstract one. The flush( ) method is another abstract method all subclasses must implement. It should force any output buffered by the stream to be written to its destination. If that destination is itself a character or byte output stream, it should invoke the flush( ) method of the destination stream as well. The close( ) method is also abstract. A subclass must implement this method so that it flushes and then closes the current stream and also closes whatever destination stream it is connected to. Once the stream is closed, any future calls to write( ) or flush( ) should throw an IOException.

In Java 5.0, this class has been modified to implement the Closeable and Flushable interfaces. It has also changed to implement java.lang.Appendable, which means that any Writer object can be used as the destination for a java.util.Formatter.

Figure 9-65. java.io.Writer


public abstract class Writer implements Appendable, Closeable, Flushable {
// Protected Constructors
     protected Writer( );  
     protected Writer(Object lock);  
// Public Instance Methods
5.0  public Writer append(char c) throws IOException;  
5.0  public Writer append(CharSequence csq) throws IOException;  
5.0  public Writer append(CharSequence csq, int start, int end) throws IOException;  
     public abstract void close( ) throws IOException; Implements:Closeable
     public abstract void flush( ) throws IOException; Implements:Flushable
     public void write(int c) throws IOException;  
     public void write(String str) throws IOException;  
     public void write(char[ ] cbuf) throws IOException;  
     public abstract void write(char[ ] cbuf, int off, int len) throws IOException;  
     public void write(String str, int off, int len) throws IOException;  
// Methods Implementing Closeable
     public abstract void close( ) throws IOException;  
// Methods Implementing Flushable
     public abstract void flush( ) throws IOException;  
// Protected Instance Fields
     protected Object lock;  
}

Subclasses

BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, PipedWriter,PrintWriter, StringWriter

Passed To

BufferedWriter.BufferedWriter( ), CharArrayWriter.writeTo( ), FilterWriter.FilterWriter( ), PrintWriter.PrintWriter( ), javax.xml.transform.stream.StreamResult.{setWriter( ), StreamResult( )}

Returned By

CharArrayWriter.append( ), PrintWriter.append( ), StringWriter.append( ), java.nio.channels.Channels.newWriter( ), javax.xml.transform.stream.StreamResult.getWriter( )

Type Of

FilterWriter.out, PrintWriter.out

    Team LiB
    Previous Section Next Section