java.io
Class Writer

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.io.Writer
All Implemented Interfaces:
Closeable sample code for java.io.Closeable definition code for java.io.Closeable , Flushable sample code for java.io.Flushable definition code for java.io.Flushable , Appendable sample code for java.lang.Appendable definition code for java.lang.Appendable
Direct Known Subclasses:
BufferedWriter sample code for java.io.BufferedWriter definition code for java.io.BufferedWriter , CharArrayWriter sample code for java.io.CharArrayWriter definition code for java.io.CharArrayWriter , FilterWriter sample code for java.io.FilterWriter definition code for java.io.FilterWriter , OutputStreamWriter sample code for java.io.OutputStreamWriter definition code for java.io.OutputStreamWriter , PipedWriter sample code for java.io.PipedWriter definition code for java.io.PipedWriter , PrintWriter sample code for java.io.PrintWriter definition code for java.io.PrintWriter , StringWriter sample code for java.io.StringWriter definition code for java.io.StringWriter

public abstract class Writer
extends Object sample code for java.lang.Object definition code for java.lang.Object
implements Appendable sample code for java.lang.Appendable definition code for java.lang.Appendable , Closeable sample code for java.io.Closeable definition code for java.io.Closeable , Flushable sample code for java.io.Flushable definition code for java.io.Flushable

Abstract class for writing to character streams. The only methods that a subclass must implement are write(char[], int, int), flush(), and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.

Since:
JDK1.1
See Also:
Writer sample code for java.io.Writer definition code for java.io.Writer , BufferedWriter sample code for java.io.BufferedWriter definition code for java.io.BufferedWriter , CharArrayWriter sample code for java.io.CharArrayWriter definition code for java.io.CharArrayWriter , FilterWriter sample code for java.io.FilterWriter definition code for java.io.FilterWriter , OutputStreamWriter sample code for java.io.OutputStreamWriter definition code for java.io.OutputStreamWriter , FileWriter sample code for java.io.FileWriter definition code for java.io.FileWriter , PipedWriter sample code for java.io.PipedWriter definition code for java.io.PipedWriter , PrintWriter sample code for java.io.PrintWriter definition code for java.io.PrintWriter , StringWriter sample code for java.io.StringWriter definition code for java.io.StringWriter , Reader sample code for java.io.Reader definition code for java.io.Reader

Field Summary
protected  Object sample code for java.lang.Object definition code for java.lang.Object lock sample code for java.io.Writer.lock definition code for java.io.Writer.lock
          The object used to synchronize operations on this stream.
 
Constructor Summary
protected Writer sample code for java.io.Writer.Writer() definition code for java.io.Writer.Writer() ()
          Create a new character-stream writer whose critical sections will synchronize on the writer itself.
protected Writer sample code for java.io.Writer.Writer(java.lang.Object) definition code for java.io.Writer.Writer(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  lock)
          Create a new character-stream writer whose critical sections will synchronize on the given object.
 
Method Summary
 Writer sample code for java.io.Writer definition code for java.io.Writer append sample code for java.io.Writer.append(char) definition code for java.io.Writer.append(char) (char c)
          Appends the specified character to this writer.
 Writer sample code for java.io.Writer definition code for java.io.Writer append sample code for java.io.Writer.append(java.lang.CharSequence) definition code for java.io.Writer.append(java.lang.CharSequence) (CharSequence sample code for java.lang.CharSequence definition code for java.lang.CharSequence  csq)
          Appends the specified character sequence to this writer.
 Writer sample code for java.io.Writer definition code for java.io.Writer append sample code for java.io.Writer.append(java.lang.CharSequence, int, int) definition code for java.io.Writer.append(java.lang.CharSequence, int, int) (CharSequence sample code for java.lang.CharSequence definition code for java.lang.CharSequence  csq, int start, int end)
          Appends a subsequence of the specified character sequence to this writer.
abstract  void close sample code for java.io.Writer.close() definition code for java.io.Writer.close() ()
          Close the stream, flushing it first.
abstract  void flush sample code for java.io.Writer.flush() definition code for java.io.Writer.flush() ()
          Flush the stream.
 void write sample code for java.io.Writer.write(char[]) definition code for java.io.Writer.write(char[]) (char[] cbuf)
          Write an array of characters.
abstract  void write sample code for java.io.Writer.write(char[], int, int) definition code for java.io.Writer.write(char[], int, int) (char[] cbuf, int off, int len)
          Write a portion of an array of characters.
 void write sample code for java.io.Writer.write(int) definition code for java.io.Writer.write(int) (int c)
          Write a single character.
 void write sample code for java.io.Writer.write(java.lang.String) definition code for java.io.Writer.write(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  str)
          Write a string.
 void write sample code for java.io.Writer.write(java.lang.String, int, int) definition code for java.io.Writer.write(java.lang.String, int, int) (String sample code for java.lang.String definition code for java.lang.String  str, int off, int len)
          Write a portion of a string.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Field Detail

lock sample code for java.io.Writer.lock

protected Object sample code for java.lang.Object definition code for java.lang.Object  lock
The object used to synchronize operations on this stream. For efficiency, a character-stream object may use an object other than itself to protect critical sections. A subclass should therefore use the object in this field rather than this or a synchronized method.

Constructor Detail

Writer sample code for java.io.Writer() definition code for java.io.Writer()

protected Writer()
Create a new character-stream writer whose critical sections will synchronize on the writer itself.


Writer sample code for java.io.Writer(java.lang.Object) definition code for java.io.Writer(java.lang.Object)

protected Writer(Object sample code for java.lang.Object definition code for java.lang.Object  lock)
Create a new character-stream writer whose critical sections will synchronize on the given object.

Parameters:
lock - Object to synchronize on.
Method Detail

write sample code for java.io.Writer.write(int) definition code for java.io.Writer.write(int)

public void write(int c)
           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Write a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.

Subclasses that intend to support efficient single-character output should override this method.

Parameters:
c - int specifying a character to be written.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

write sample code for java.io.Writer.write(char[]) definition code for java.io.Writer.write(char[])

public void write(char[] cbuf)
           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Write an array of characters.

Parameters:
cbuf - Array of characters to be written
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

write sample code for java.io.Writer.write(char[], int, int) definition code for java.io.Writer.write(char[], int, int)

public abstract void write(char[] cbuf,
                           int off,
                           int len)
                    throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Write a portion of an array of characters.

Parameters:
cbuf - Array of characters
off - Offset from which to start writing characters
len - Number of characters to write
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

write sample code for java.io.Writer.write(java.lang.String) definition code for java.io.Writer.write(java.lang.String)

public void write(String sample code for java.lang.String definition code for java.lang.String  str)
           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Write a string.

Parameters:
str - String to be written
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

write sample code for java.io.Writer.write(java.lang.String, int, int) definition code for java.io.Writer.write(java.lang.String, int, int)

public void write(String sample code for java.lang.String definition code for java.lang.String  str,
                  int off,
                  int len)
           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Write a portion of a string.

Parameters:
str - A String
off - Offset from which to start writing characters
len - Number of characters to write
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

append sample code for java.io.Writer.append(java.lang.CharSequence) definition code for java.io.Writer.append(java.lang.CharSequence)

public Writer sample code for java.io.Writer definition code for java.io.Writer  append(CharSequence sample code for java.lang.CharSequence definition code for java.lang.CharSequence  csq)
              throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Appends the specified character sequence to this writer.

An invocation of this method of the form out.append(csq) behaves in exactly the same way as the invocation

     out.write(csq.toString()) 

Depending on the specification of toString for the character sequence csq, the entire sequence may not be appended. For instance, invoking the toString method of a character buffer will return a subsequence whose content depends upon the buffer's position and limit.

Specified by:
append sample code for java.lang.Appendable.append(java.lang.CharSequence) definition code for java.lang.Appendable.append(java.lang.CharSequence) in interface Appendable sample code for java.lang.Appendable definition code for java.lang.Appendable
Parameters:
csq - The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.
Returns:
This writer
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs
Since:
1.5

append sample code for java.io.Writer.append(java.lang.CharSequence, int, int) definition code for java.io.Writer.append(java.lang.CharSequence, int, int)

public Writer sample code for java.io.Writer definition code for java.io.Writer  append(CharSequence sample code for java.lang.CharSequence definition code for java.lang.CharSequence  csq,
                     int start,
                     int end)
              throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Appends a subsequence of the specified character sequence to this writer. Appendable.

An invocation of this method of the form out.append(csq, start, end) when csq is not null behaves in exactly the same way as the invocation

     out.write(csq.subSequence(start, end).toString()) 

Specified by:
append sample code for java.lang.Appendable.append(java.lang.CharSequence, int, int) definition code for java.lang.Appendable.append(java.lang.CharSequence, int, int) in interface Appendable sample code for java.lang.Appendable definition code for java.lang.Appendable
Parameters:
csq - The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".
start - The index of the first character in the subsequence
end - The index of the character following the last character in the subsequence
Returns:
This writer
Throws:
IndexOutOfBoundsException sample code for java.lang.IndexOutOfBoundsException definition code for java.lang.IndexOutOfBoundsException - If start or end are negative, start is greater than end, or end is greater than csq.length()
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs
Since:
1.5

append sample code for java.io.Writer.append(char) definition code for java.io.Writer.append(char)

public Writer sample code for java.io.Writer definition code for java.io.Writer  append(char c)
              throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Appends the specified character to this writer.

An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation

     out.write(c) 

Specified by:
append sample code for java.lang.Appendable.append(char) definition code for java.lang.Appendable.append(char) in interface Appendable sample code for java.lang.Appendable definition code for java.lang.Appendable
Parameters:
c - The 16-bit character to append
Returns:
This writer
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs
Since:
1.5

flush sample code for java.io.Writer.flush() definition code for java.io.Writer.flush()

public abstract void flush()
                    throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Flush the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Specified by:
flush sample code for java.io.Flushable.flush() definition code for java.io.Flushable.flush() in interface Flushable sample code for java.io.Flushable definition code for java.io.Flushable
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

close sample code for java.io.Writer.close() definition code for java.io.Writer.close()

public abstract void close()
                    throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Close the stream, flushing it first. Once a stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect.

Specified by:
close sample code for java.io.Closeable.close() definition code for java.io.Closeable.close() in interface Closeable sample code for java.io.Closeable definition code for java.io.Closeable
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs