java.io
Class OutputStreamWriter

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.io.Writer sample code for java.io.Writer definition code for java.io.Writer 
      extended by java.io.OutputStreamWriter
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:
FileWriter sample code for java.io.FileWriter definition code for java.io.FileWriter

public class OutputStreamWriter
extends Writer sample code for java.io.Writer definition code for java.io.Writer

An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset . The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

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));
 

A surrogate pair is a character represented by a sequence of two char values: A high surrogate in the range '\uD800' to '\uDBFF' followed by a low surrogate in the range '\uDC00' to '\uDFFF'. If the character represented by a surrogate pair cannot be encoded by a given charset then a charset-dependent substitution sequence is written to the output stream.

A malformed surrogate element is a high surrogate that is not followed by a low surrogate or a low surrogate that is not preceded by a high surrogate. It is illegal to attempt to write a character stream containing malformed surrogate elements. The behavior of an instance of this class when a malformed surrogate element is written is not specified.

Since:
JDK1.1
See Also:
BufferedWriter sample code for java.io.BufferedWriter definition code for java.io.BufferedWriter , OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream , Charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset

Field Summary
 
Fields inherited from class java.io.Writer sample code for java.io.Writer definition code for java.io.Writer
lock sample code for java.io.Writer.lock definition code for java.io.Writer.lock
 
Constructor Summary
OutputStreamWriter sample code for java.io.OutputStreamWriter.OutputStreamWriter(java.io.OutputStream) definition code for java.io.OutputStreamWriter.OutputStreamWriter(java.io.OutputStream) (OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out)
          Create an OutputStreamWriter that uses the default character encoding.
OutputStreamWriter sample code for java.io.OutputStreamWriter.OutputStreamWriter(java.io.OutputStream, java.nio.charset.Charset) definition code for java.io.OutputStreamWriter.OutputStreamWriter(java.io.OutputStream, java.nio.charset.Charset) (OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out, Charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset  cs)
          Create an OutputStreamWriter that uses the given charset.
OutputStreamWriter sample code for java.io.OutputStreamWriter.OutputStreamWriter(java.io.OutputStream, java.nio.charset.CharsetEncoder) definition code for java.io.OutputStreamWriter.OutputStreamWriter(java.io.OutputStream, java.nio.charset.CharsetEncoder) (OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out, CharsetEncoder sample code for java.nio.charset.CharsetEncoder definition code for java.nio.charset.CharsetEncoder  enc)
          Create an OutputStreamWriter that uses the given charset encoder.
OutputStreamWriter sample code for java.io.OutputStreamWriter.OutputStreamWriter(java.io.OutputStream, java.lang.String) definition code for java.io.OutputStreamWriter.OutputStreamWriter(java.io.OutputStream, java.lang.String) (OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out, String sample code for java.lang.String definition code for java.lang.String  charsetName)
          Create an OutputStreamWriter that uses the named charset.
 
Method Summary
 void close sample code for java.io.OutputStreamWriter.close() definition code for java.io.OutputStreamWriter.close() ()
          Close the stream.
 void flush sample code for java.io.OutputStreamWriter.flush() definition code for java.io.OutputStreamWriter.flush() ()
          Flush the stream.
 String sample code for java.lang.String definition code for java.lang.String getEncoding sample code for java.io.OutputStreamWriter.getEncoding() definition code for java.io.OutputStreamWriter.getEncoding() ()
          Return the name of the character encoding being used by this stream.
 void write sample code for java.io.OutputStreamWriter.write(char[], int, int) definition code for java.io.OutputStreamWriter.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.OutputStreamWriter.write(int) definition code for java.io.OutputStreamWriter.write(int) (int c)
          Write a single character.
 void write sample code for java.io.OutputStreamWriter.write(java.lang.String, int, int) definition code for java.io.OutputStreamWriter.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.io.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) , append sample code for java.io.Writer.append(java.lang.CharSequence) definition code for java.io.Writer.append(java.lang.CharSequence) , 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) , write sample code for java.io.Writer.write(char[]) definition code for java.io.Writer.write(char[]) , write sample code for java.io.Writer.write(java.lang.String) definition code for java.io.Writer.write(java.lang.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)
 

Constructor Detail

OutputStreamWriter sample code for java.io.OutputStreamWriter(java.io.OutputStream, java.lang.String) definition code for java.io.OutputStreamWriter(java.io.OutputStream, java.lang.String)

public OutputStreamWriter(OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out,
                          String sample code for java.lang.String definition code for java.lang.String  charsetName)
                   throws UnsupportedEncodingException sample code for java.io.UnsupportedEncodingException definition code for java.io.UnsupportedEncodingException 
Create an OutputStreamWriter that uses the named charset.

Parameters:
out - An OutputStream
charsetName - The name of a supported charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset
Throws:
UnsupportedEncodingException sample code for java.io.UnsupportedEncodingException definition code for java.io.UnsupportedEncodingException - If the named encoding is not supported

OutputStreamWriter sample code for java.io.OutputStreamWriter(java.io.OutputStream) definition code for java.io.OutputStreamWriter(java.io.OutputStream)

public OutputStreamWriter(OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out)
Create an OutputStreamWriter that uses the default character encoding.

Parameters:
out - An OutputStream

OutputStreamWriter sample code for java.io.OutputStreamWriter(java.io.OutputStream, java.nio.charset.Charset) definition code for java.io.OutputStreamWriter(java.io.OutputStream, java.nio.charset.Charset)

public OutputStreamWriter(OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out,
                          Charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset  cs)
Create an OutputStreamWriter that uses the given charset.

Parameters:
out - An OutputStream
cs - A charset
Since:
1.4

OutputStreamWriter sample code for java.io.OutputStreamWriter(java.io.OutputStream, java.nio.charset.CharsetEncoder) definition code for java.io.OutputStreamWriter(java.io.OutputStream, java.nio.charset.CharsetEncoder)

public OutputStreamWriter(OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out,
                          CharsetEncoder sample code for java.nio.charset.CharsetEncoder definition code for java.nio.charset.CharsetEncoder  enc)
Create an OutputStreamWriter that uses the given charset encoder.

Parameters:
out - An OutputStream
enc - A charset encoder
Since:
1.4
Method Detail

getEncoding sample code for java.io.OutputStreamWriter.getEncoding() definition code for java.io.OutputStreamWriter.getEncoding()

public String sample code for java.lang.String definition code for java.lang.String  getEncoding()
Return the name of the character encoding being used by this stream.

If the encoding has an historical name then that name is returned; otherwise the encoding's canonical name is returned.

If this instance was created with the OutputStreamWriter(OutputStream, String) sample code for java.io.OutputStreamWriter.OutputStreamWriter(java.io.OutputStream, java.lang.String) definition code for java.io.OutputStreamWriter.OutputStreamWriter(java.io.OutputStream, java.lang.String) constructor then the returned name, being unique for the encoding, may differ from the name passed to the constructor. This method may return null if the stream has been closed.

Returns:
The historical name of this encoding, or possibly null if the stream has been closed
See Also:
Charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset

write sample code for java.io.OutputStreamWriter.write(int) definition code for java.io.OutputStreamWriter.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.

Overrides:
write sample code for java.io.Writer.write(int) definition code for java.io.Writer.write(int) in class Writer sample code for java.io.Writer definition code for java.io.Writer
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.OutputStreamWriter.write(char[], int, int) definition code for java.io.OutputStreamWriter.write(char[], int, int)

public 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.

Specified by:
write sample code for java.io.Writer.write(char[], int, int) definition code for java.io.Writer.write(char[], int, int) in class Writer sample code for java.io.Writer definition code for java.io.Writer
Parameters:
cbuf - Buffer 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.OutputStreamWriter.write(java.lang.String, int, int) definition code for java.io.OutputStreamWriter.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.

Overrides:
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) in class Writer sample code for java.io.Writer definition code for java.io.Writer
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

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

public void flush()
           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Flush the stream.

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
Specified by:
flush sample code for java.io.Writer.flush() definition code for java.io.Writer.flush() in class Writer sample code for java.io.Writer definition code for java.io.Writer
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.OutputStreamWriter.close() definition code for java.io.OutputStreamWriter.close()

public void close()
           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Close the stream.

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
Specified by:
close sample code for java.io.Writer.close() definition code for java.io.Writer.close() in class Writer sample code for java.io.Writer definition code for java.io.Writer
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs