Package org.fife.io
Class UnicodeWriter
- java.lang.Object
-
- java.io.Writer
-
- org.fife.io.UnicodeWriter
-
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
public class UnicodeWriter extends Writer
Writes Unicode text to an output stream. If the specified encoding is a Unicode, then the text is preceded by the proper Unicode BOM. If it is any other encoding, this class behaves just likeOutputStreamWriter. This class is here because Java'sOutputStreamWriterapparently doesn't believe in writing BOMs.For optimum performance, it is recommended that you wrap all instances of
UnicodeWriterwith ajava.io.BufferedWriter.
-
-
Field Summary
Fields Modifier and Type Field Description static StringPROPERTY_WRITE_UTF8_BOMIf this system property evaluates to "false", ignoring case, files written out as UTF-8 will not have a BOM written for them.
-
Constructor Summary
Constructors Constructor Description UnicodeWriter(File file, String encoding)This is a utility constructor since the vast majority of the time, this class will be used to write Unicode files.UnicodeWriter(OutputStream out, String encoding)Creates a new writer.UnicodeWriter(String fileName, String encoding)This is a utility constructor since the vast majority of the time, this class will be used to write Unicode files.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this writer.voidflush()Flushes the stream.StringgetEncoding()Returns the encoding being used to write this output stream (i.e., the encoding of the file).static booleangetWriteUtf8BOM()Returns whether UTF-8 files should have a BOM in them when written.static voidsetWriteUtf8BOM(boolean write)Sets whether UTF-8 files should have a BOM written in them.voidwrite(char[] cbuf, int off, int len)Writes a portion of an array of characters.voidwrite(int c)Writes a single character.voidwrite(String str, int off, int len)Writes a portion of a string.
-
-
-
Field Detail
-
PROPERTY_WRITE_UTF8_BOM
public static final String PROPERTY_WRITE_UTF8_BOM
If this system property evaluates to "false", ignoring case, files written out as UTF-8 will not have a BOM written for them. Otherwise (even if the property is not set), UTF-8 files will have a BOM written.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
UnicodeWriter
public UnicodeWriter(String fileName, String encoding) throws UnsupportedEncodingException, IOException
This is a utility constructor since the vast majority of the time, this class will be used to write Unicode files.- Parameters:
fileName- The file to which to write the Unicode output.encoding- The encoding to use.- Throws:
UnsupportedEncodingException- If the specified encoding is not supported.IOException- If an IO exception occurs.
-
UnicodeWriter
public UnicodeWriter(File file, String encoding) throws UnsupportedEncodingException, IOException
This is a utility constructor since the vast majority of the time, this class will be used to write Unicode files.- Parameters:
file- The file to which to write the Unicode output.encoding- The encoding to use.- Throws:
UnsupportedEncodingException- If the specified encoding is not supported.IOException- If an IO exception occurs.
-
UnicodeWriter
public UnicodeWriter(OutputStream out, String encoding) throws UnsupportedEncodingException, IOException
Creates a new writer.- Parameters:
out- The output stream to write.encoding- The encoding to use.- Throws:
UnsupportedEncodingException- If the specified encoding is not supported.IOException- If an IO exception occurs.
-
-
Method Detail
-
close
public void close() throws IOExceptionCloses this writer.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classWriter- Throws:
IOException- If an IO exception occurs.
-
flush
public void flush() throws IOExceptionFlushes the stream.- Specified by:
flushin interfaceFlushable- Specified by:
flushin classWriter- Throws:
IOException- If an IO exception occurs.
-
getEncoding
public String getEncoding()
Returns the encoding being used to write this output stream (i.e., the encoding of the file).- Returns:
- The encoding of the stream.
-
getWriteUtf8BOM
public static boolean getWriteUtf8BOM()
Returns whether UTF-8 files should have a BOM in them when written.- Returns:
- Whether to write a BOM for UTF-8 files.
- See Also:
setWriteUtf8BOM(boolean),UnicodeWriter
-
setWriteUtf8BOM
public static void setWriteUtf8BOM(boolean write)
Sets whether UTF-8 files should have a BOM written in them.- Parameters:
write- Whether to write a BOM.- See Also:
getWriteUtf8BOM(),UnicodeWriter
-
write
public void write(char[] cbuf, int off, int len) throws IOExceptionWrites a portion of an array of characters.- Specified by:
writein classWriter- Parameters:
cbuf- The buffer of characters.off- The offset from which to start writing characters.len- The number of characters to write.- Throws:
IOException- If an I/O error occurs.
-
write
public void write(int c) throws IOExceptionWrites a single character.- Overrides:
writein classWriter- Parameters:
c- An integer specifying the character to write.- Throws:
IOException- If an IO error occurs.
-
write
public void write(String str, int off, int len) throws IOException
Writes a portion of a string.- Overrides:
writein classWriter- Parameters:
str- The string from which to write.off- The offset from which to start writing characters.len- The number of characters to write.- Throws:
IOException- If an IO error occurs.
-
-