All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.io.OutputStreamWriter

java.lang.Object
   |
   +----java.io.Writer
           |
           +----java.io.OutputStreamWriter

public class OutputStreamWriter
extends Writer
Write characters to an output stream, translating characters into bytes according to a specified character encoding. Each OutputStreamWriter incorporates its own CharToByteConverter, and is thus a bridge from character streams to byte streams.

The encoding used by an OutputStreamWriter may be specified by name, by providing a CharToByteConverter, or by accepting the default encoding, which is defined by the system property file.encoding.

Each invocation of a write() method causes the encoding converter to be invoked on the given character(s). The resulting bytes are accumulated in a buffer before being written to the underlying output stream. The size of this buffer may be specified, but by default it is large enough for most purposes. Note that the characters passed to the write() methods are not buffered. For top efficiency, consider wrapping an OutputStreamWriter within a BufferedWriter so as to avoid frequent converter invocations. For example,

 Writer out
   = new BufferedWriter(new OutputStreamWriter(System.out));
 

See Also:
BufferedWriter, OutputStream

Constructor Index

 o OutputStreamWriter(OutputStream)
Create an OutputStreamWriter that uses the default character encoding and the default output-buffer size.
 o OutputStreamWriter(OutputStream, CharToByteConverter)
Create an OutputStreamWriter that uses the specified character-to-byte converter and the default output-buffer size.
 o OutputStreamWriter(OutputStream, CharToByteConverter, int)
Create an OutputStreamWriter that uses the specified character-to-byte converter and the specified output-buffer size.
 o OutputStreamWriter(OutputStream, int)
Create an OutputStreamWriter that uses the default character encoding and the specified output-buffer size.
 o OutputStreamWriter(OutputStream, String)
Create an OutputStreamWriter that uses the named character encoding and the default output-buffer size.
 o OutputStreamWriter(OutputStream, String, int)
Create an OutputStreamWriter that uses the named character encoding and the specified output-buffer size.

Method Index

 o close()
Close the stream.
 o flush()
Flush the stream.
 o getCharacterEncoding()
Return the name of the encoding being used by this stream.
 o write(char[], int, int)
Write a portion of an array of characters.
 o write(int)
Write a single character.
 o write(String, int, int)
Write a portion of a string.

Constructors

 o OutputStreamWriter
  public OutputStreamWriter(OutputStream out)
Create an OutputStreamWriter that uses the default character encoding and the default output-buffer size.

Parameters:
out - An OutputStream
 o OutputStreamWriter
  public OutputStreamWriter(OutputStream out,
                            String enc) throws UnsupportedEncodingException
Create an OutputStreamWriter that uses the named character encoding and the default output-buffer size.

Parameters:
out - An OutputStream
enc - Name of the encoding to be used
Throws: UnsupportedEncodingException
If no converter can be found for the named encoding
 o OutputStreamWriter
  public OutputStreamWriter(OutputStream out,
                            int sz)
Create an OutputStreamWriter that uses the default character encoding and the specified output-buffer size.

Parameters:
out - An OutputStream
sz - Output-buffer size
Throws: IllegalArgumentException
If sz is <= 0
 o OutputStreamWriter
  public OutputStreamWriter(OutputStream out,
                            String enc,
                            int sz) throws UnsupportedEncodingException
Create an OutputStreamWriter that uses the named character encoding and the specified output-buffer size.

Parameters:
out - An OutputStream
enc - Name of encoding to be used
sz - Output-buffer size
Throws: UnsupportedEncodingException
If no converter can be found for the specified encoding
Throws: IllegalArgumentException
If sz is <= 0
 o OutputStreamWriter
  public OutputStreamWriter(OutputStream out,
                            CharToByteConverter ctb)
Create an OutputStreamWriter that uses the specified character-to-byte converter and the default output-buffer size. The converter will be reset before its first use. Note: Because the converter has state, it should not be used after the constructor returns.

Parameters:
out - An OutputStream
ctb - A CharToByteConverter
 o OutputStreamWriter
  public OutputStreamWriter(OutputStream out,
                            CharToByteConverter ctb,
                            int sz)
Create an OutputStreamWriter that uses the specified character-to-byte converter and the specified output-buffer size. The converter will be reset before its first use. Note: Because the converter has state, it should not be used after the constructor returns.

Parameters:
out - An OutputStream
ctb - A CharToByteConverter
sz - Output-buffer size
Throws: IllegalArgumentException
If sz is <= 0

Methods

 o getCharacterEncoding
  public String getCharacterEncoding() throws IOException
Return the name of the encoding being used by this stream.

Throws: IOException
If an I/O error occurs
 o write
  public void write(int c) throws IOException
Write a single character.

Throws: IOException
If an I/O error occurs
Overrides:
write in class Writer
 o write
  public void write(char cbuf[],
                    int off,
                    int len) throws IOException
Write a portion of an array of characters.

Parameters:
cbuf - Buffer of characters
off - Offset from which to start writing characters
len - Number of characters to write
Throws: ConversionBufferFullException
If the output buffer is too small for even one converted character
Throws: IOException
If some other I/O error occurs
Overrides:
write in class Writer
 o write
  public void write(String str,
                    int off,
                    int len) throws 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
If an I/O error occurs
Overrides:
write in class Writer
 o flush
  public void flush() throws IOException
Flush the stream.

Throws: IOException
If an I/O error occurs
Overrides:
flush in class Writer
 o close
  public void close() throws IOException
Close the stream.

Throws: IOException
If an I/O error occurs
Overrides:
close in class Writer

All Packages  Class Hierarchy  This Package  Previous  Next  Index