public class OutputStreamWriter extends Writer
OutputStreamWriter
contains a buffer
of bytes to be written to target stream and converts these into characters as
needed. The buffer size is 8K.InputStreamReader
Constructor and Description |
---|
OutputStreamWriter(OutputStream out)
Constructs a new OutputStreamWriter using
out as the target
stream to write converted characters to. |
OutputStreamWriter(OutputStream out,
Charset cs)
Constructs a new OutputStreamWriter using
out as the target
stream to write converted characters to and cs as the character
encoding. |
OutputStreamWriter(OutputStream out,
CharsetEncoder charsetEncoder)
Constructs a new OutputStreamWriter using
out as the target
stream to write converted characters to and charsetEncoder as the character
encoder. |
OutputStreamWriter(OutputStream out,
String charsetName)
Constructs a new OutputStreamWriter using
out as the target
stream to write converted characters to and charsetName as the character
encoding. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this writer.
|
void |
flush()
Flushes this writer.
|
String |
getEncoding()
Returns the historical name of the encoding used by this writer to convert characters to
bytes, or null if this writer has been closed.
|
void |
write(char[] buffer,
int offset,
int count)
Writes
count characters starting at offset in buf
to this writer. |
void |
write(int oneChar)
Writes the character
oneChar to this writer. |
void |
write(String str,
int offset,
int count)
Writes
count characters starting at offset in str
to this writer. |
public OutputStreamWriter(OutputStream out)
out
as the target
stream to write converted characters to. The default character encoding
is used.out
- the non-null target stream to write converted bytes to.public OutputStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException
out
as the target
stream to write converted characters to and charsetName
as the character
encoding. If the encoding cannot be found, an
UnsupportedEncodingException error is thrown.out
- the target stream to write converted bytes to.charsetName
- the string describing the desired character encoding.NullPointerException
- if charsetName
is null
.UnsupportedEncodingException
- if the encoding specified by charsetName
cannot be found.public OutputStreamWriter(OutputStream out, Charset cs)
out
as the target
stream to write converted characters to and cs
as the character
encoding.out
- the target stream to write converted bytes to.cs
- the Charset
that specifies the character encoding.public OutputStreamWriter(OutputStream out, CharsetEncoder charsetEncoder)
out
as the target
stream to write converted characters to and charsetEncoder
as the character
encoder.out
- the target stream to write converted bytes to.charsetEncoder
- the character encoder used for character conversion.public void close() throws IOException
Only the first invocation of this method has any effect. Subsequent calls do nothing.
close
in interface Closeable
close
in interface AutoCloseable
close
in class Writer
IOException
- if an error occurs while closing this writer.public void flush() throws IOException
flush
in interface Flushable
flush
in class Writer
IOException
- if an error occurs while flushing this writer.public String getEncoding()
public void write(char[] buffer, int offset, int count) throws IOException
count
characters starting at offset
in buf
to this writer. The characters are immediately converted to bytes by the
character converter and stored in a local buffer. If the buffer gets full
as a result of the conversion, this writer is flushed.write
in class Writer
buffer
- the array containing characters to write.offset
- the index of the first character in buf
to write.count
- the maximum number of characters to write.IndexOutOfBoundsException
- if offset < 0
or count < 0
, or if
offset + count
is greater than the size of
buf
.IOException
- if this writer has already been closed or another I/O error
occurs.public void write(int oneChar) throws IOException
oneChar
to this writer. The lowest two bytes
of the integer oneChar
are immediately converted to bytes by the
character converter and stored in a local buffer. If the buffer gets full
by converting this character, this writer is flushed.write
in class Writer
oneChar
- the character to write.IOException
- if this writer is closed or another I/O error occurs.public void write(String str, int offset, int count) throws IOException
count
characters starting at offset
in str
to this writer. The characters are immediately converted to bytes by the
character converter and stored in a local buffer. If the buffer gets full
as a result of the conversion, this writer is flushed.write
in class Writer
str
- the string containing characters to write.offset
- the start position in str
for retrieving characters.count
- the maximum number of characters to write.IOException
- if this writer has already been closed or another I/O error
occurs.IndexOutOfBoundsException
- if offset < 0
or count < 0
, or if
offset + count
is bigger than the length of
str
.