Top Description Fields Constructors Methods
java.io

public Class OutputStreamWriter

extends Writer
Class Inheritance
Known Direct Subclasses
java.io.FileWriter
Imports
java.nio.CharBuffer, java.nio.charset.Charset, .CharsetEncoder, jdk.internal.misc.InternalLock, sun.nio.cs.StreamEncoder

An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the 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. 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(anOutputStream));
Writer out = new BufferedWriter(new OutputStreamWriter(anOutputStream));

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

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.

This class always replaces malformed surrogate elements and unmappable character sequences with the charset's default substitution sequence. The CharsetEncoder class should be used when more control over the encoding process is required.

Author
Mark Reinhold
Since
1.1
See Also
BufferedWriter, OutputStream, Charset

Field Summary

Modifier and TypeField and Description
private final StreamEncoder
Inherited from java.io.Writer:
lock

Constructor Summary

AccessConstructor and Description
public
OutputStreamWriter(OutputStream
An OutputStream
out
,
String
The name of a supported charset
charsetName
)

Creates an OutputStreamWriter that uses the named charset.

public
OutputStreamWriter(OutputStream
An OutputStream
out
)

Creates an OutputStreamWriter that uses the default character encoding, or where out is a PrintStream, the charset used by the print stream.

public
OutputStreamWriter(OutputStream
An OutputStream
out
,
Charset
A charset
cs
)

Creates an OutputStreamWriter that uses the given charset.

public
OutputStreamWriter(OutputStream
An OutputStream
out
,
CharsetEncoder
A charset encoder
enc
)

Creates an OutputStreamWriter that uses the given charset encoder.

Method Summary

Modifier and TypeMethod and Description
public Writer
append(CharSequence
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".
csq
,
int
The index of the first character in the subsequence
start
,
int
The index of the character following the last character in the subsequence
end
)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Appends a subsequence of the specified character sequence to this writer.

public Writer
append(CharSequence
The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.
csq
)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Appends the specified character sequence to this writer.

public void
close()

Implements abstract java.io.Writer.close.

Implements java.io.Closeable.close.

Closes the stream, flushing it first.

public void
flush()

Implements abstract java.io.Writer.flush.

Implements java.io.Flushable.flush.

Flushes the stream.

pack-priv void
flushBuffer()

Flushes the output buffer to the underlying byte stream, without flushing the byte stream itself.

public String

Returns:

The historical name of this encoding, or possibly null if the stream has been closed
getEncoding
()

Returns the name of the character encoding being used by this stream.

private static Object
lockFor(OutputStreamWriter writer)

Return the lock object for the given writer's stream encoder.

public void
write(int
int specifying a character to be written
c
)

Overrides java.io.Writer.write.

Writes a single character.

public void
write(char[]
Buffer of characters
cbuf
,
int
Offset from which to start writing characters
off
,
int
Number of characters to write
len
)

Implements abstract java.io.Writer.write.

Writes a portion of an array of characters.

public void
write(String
A String
str
,
int
Offset from which to start writing characters
off
,
int
Number of characters to write
len
)

Overrides java.io.Writer.write.

Writes a portion of a string.

Inherited from java.io.Writer:
appendnullWriterwritewrite

Field Detail

seback to summary
private final StreamEncoder se

Constructor Detail

OutputStreamWriterback to summary
public OutputStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException

Creates an OutputStreamWriter that uses the named charset.

Parameters
out:OutputStream

An OutputStream

charsetName:String

The name of a supported charset

Exceptions
UnsupportedEncodingException:
If the named encoding is not supported
OutputStreamWriterback to summary
public OutputStreamWriter(OutputStream out)

Creates an OutputStreamWriter that uses the default character encoding, or where out is a PrintStream, the charset used by the print stream.

Parameters
out:OutputStream

An OutputStream

See Also
Charset#defaultCharset()
OutputStreamWriterback to summary
public OutputStreamWriter(OutputStream out, Charset cs)

Creates an OutputStreamWriter that uses the given charset.

Parameters
out:OutputStream

An OutputStream

cs:Charset

A charset

Since
1.4
OutputStreamWriterback to summary
public OutputStreamWriter(OutputStream out, CharsetEncoder enc)

Creates an OutputStreamWriter that uses the given charset encoder.

Parameters
out:OutputStream

An OutputStream

enc:CharsetEncoder

A charset encoder

Since
1.4

Method Detail

appendback to summary
public Writer append(CharSequence csq, int start, int end) throws IOException

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Doc from java.io.Writer.append.

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())
out.write(csq.subSequence(start, end).toString())
Parameters
csq:CharSequence

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:int

The index of the first character in the subsequence

end:int

The index of the character following the last character in the subsequence

Returns:Writer

This writer

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
appendback to summary
public Writer append(CharSequence csq) throws IOException

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Doc from java.io.Writer.append.

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())
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.

Parameters
csq:CharSequence

The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.

Returns:Writer

This writer

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
closeback to summary
public void close() throws IOException

Implements abstract java.io.Writer.close.

Implements java.io.Closeable.close.

Doc from java.io.Writer.close.

Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.

Exceptions
IOException:
If an I/O error occurs
flushback to summary
public void flush() throws IOException

Implements abstract java.io.Writer.flush.

Implements java.io.Flushable.flush.

Flushes the stream.

Exceptions
IOException:
If an I/O error occurs
flushBufferback to summary
pack-priv void flushBuffer() throws IOException

Flushes the output buffer to the underlying byte stream, without flushing the byte stream itself. This method is non-private only so that it may be invoked by PrintStream.

getEncodingback to summary
public String getEncoding()

Returns 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) 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:String

The historical name of this encoding, or possibly null if the stream has been closed

See Also
Charset
lockForback to summary
private static Object lockFor(OutputStreamWriter writer)

Return the lock object for the given writer's stream encoder. If the writer type is trusted then an internal lock can be used. If the writer type is not trusted then the writer object is the lock.

writeback to summary
public void write(int c) throws IOException

Overrides java.io.Writer.write.

Writes a single character.

Parameters
c:int

Doc from java.io.Writer.write.

int specifying a character to be written

Exceptions
IOException:
If an I/O error occurs
writeback to summary
public void write(char[] cbuf, int off, int len) throws IOException

Implements abstract java.io.Writer.write.

Writes a portion of an array of characters.

Parameters
cbuf:char[]

Buffer of characters

off:int

Offset from which to start writing characters

len:int

Number of characters to write

Exceptions
IOException:
If an I/O error occurs
IndexOutOfBoundsException:
If off is negative, or len is negative, or off + len is negative or greater than the length of the given array
writeback to summary
public void write(String str, int off, int len) throws IOException

Overrides java.io.Writer.write.

Writes a portion of a string.

Parameters
str:String

A String

off:int

Offset from which to start writing characters

len:int

Number of characters to write

Exceptions
IOException:
If an I/O error occurs
IndexOutOfBoundsException:
If off is negative, or len is negative, or off + len is negative or greater than the length of the given string