Package java.io
Class OutputStreamWriter
- java.lang.Object
-
- java.io.Writer
-
- java.io.OutputStreamWriter
-
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
- Direct Known Subclasses:
FileWriter
public class OutputStreamWriter extends Writer
A class for turning a character stream into a byte stream. Data written to the target input stream is converted into bytes by either a default or a provided character converter. The default encoding is taken from the "file.encoding" system property.OutputStreamWritercontains a buffer of bytes to be written to target stream and converts these into characters as needed. The buffer size is 8K.- See Also:
InputStreamReader
-
-
Constructor Summary
Constructors Constructor Description OutputStreamWriter(OutputStream out)Constructs a new OutputStreamWriter usingoutas the target stream to write converted characters to.OutputStreamWriter(OutputStream out, String charsetName)Constructs a new OutputStreamWriter usingoutas the target stream to write converted characters to andcharsetNameas the character encoding.OutputStreamWriter(OutputStream out, Charset cs)Constructs a new OutputStreamWriter usingoutas the target stream to write converted characters to andcsas the character encoding.OutputStreamWriter(OutputStream out, CharsetEncoder charsetEncoder)Constructs a new OutputStreamWriter usingoutas the target stream to write converted characters to andcharsetEncoderas the character encoder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this writer.voidflush()Flushes this writer.StringgetEncoding()Returns the canonical name of the encoding used by this writer to convert characters to bytes, or null if this writer has been closed.voidwrite(char[] buffer, int offset, int count)Writescountcharacters starting atoffsetinbufto this writer.voidwrite(int oneChar)Writes the characteroneCharto this writer.voidwrite(String str, int offset, int count)Writescountcharacters starting atoffsetinstrto this writer.
-
-
-
Constructor Detail
-
OutputStreamWriter
public OutputStreamWriter(OutputStream out)
Constructs a new OutputStreamWriter usingoutas the target stream to write converted characters to. The default character encoding is used.- Parameters:
out- the non-null target stream to write converted bytes to.
-
OutputStreamWriter
public OutputStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException
Constructs a new OutputStreamWriter usingoutas the target stream to write converted characters to andcharsetNameas the character encoding. If the encoding cannot be found, an UnsupportedEncodingException error is thrown.- Parameters:
out- the target stream to write converted bytes to.charsetName- the string describing the desired character encoding.- Throws:
NullPointerException- ifcharsetNameisnull.UnsupportedEncodingException- if the encoding specified bycharsetNamecannot be found.
-
OutputStreamWriter
public OutputStreamWriter(OutputStream out, Charset cs)
Constructs a new OutputStreamWriter usingoutas the target stream to write converted characters to andcsas the character encoding.- Parameters:
out- the target stream to write converted bytes to.cs- theCharsetthat specifies the character encoding.
-
OutputStreamWriter
public OutputStreamWriter(OutputStream out, CharsetEncoder charsetEncoder)
Constructs a new OutputStreamWriter usingoutas the target stream to write converted characters to andcharsetEncoderas the character encoder.- Parameters:
out- the target stream to write converted bytes to.charsetEncoder- the character encoder used for character conversion.
-
-
Method Detail
-
close
public void close() throws IOExceptionCloses this writer. This implementation flushes the buffer but does not flush the target stream. The target stream is then closed and the resources for the buffer and converter are released.Only the first invocation of this method has any effect. Subsequent calls do nothing.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classWriter- Throws:
IOException- if an error occurs while closing this writer.
-
flush
public void flush() throws IOExceptionFlushes this writer. This implementation ensures that all buffered bytes are written to the target stream. After writing the bytes, the target stream is flushed as well.- Specified by:
flushin interfaceFlushable- Specified by:
flushin classWriter- Throws:
IOException- if an error occurs while flushing this writer.
-
getEncoding
public String getEncoding()
Returns the canonical name of the encoding used by this writer to convert characters to bytes, or null if this writer has been closed. Most callers should probably keep track of the String or Charset they passed in; this method may not return the same name.
-
write
public void write(char[] buffer, int offset, int count) throws IOExceptionWritescountcharacters starting atoffsetinbufto 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.- Specified by:
writein classWriter- Parameters:
buffer- the array containing characters to write.offset- the index of the first character inbufto write.count- the maximum number of characters to write.- Throws:
IndexOutOfBoundsException- ifoffset < 0orcount < 0, or ifoffset + countis greater than the size ofbuf.IOException- if this writer has already been closed or another I/O error occurs.
-
write
public void write(int oneChar) throws IOExceptionWrites the characteroneCharto this writer. The lowest two bytes of the integeroneCharare 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.- Overrides:
writein classWriter- Parameters:
oneChar- the character to write.- Throws:
IOException- if this writer is closed or another I/O error occurs.
-
write
public void write(String str, int offset, int count) throws IOException
Writescountcharacters starting atoffsetinstrto 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.- Overrides:
writein classWriter- Parameters:
str- the string containing characters to write.offset- the start position instrfor retrieving characters.count- the maximum number of characters to write.- Throws:
IOException- if this writer has already been closed or another I/O error occurs.IndexOutOfBoundsException- ifoffset < 0orcount < 0, or ifoffset + countis bigger than the length ofstr.
-
-