Class UnicodeWriter
java.lang.Object
java.io.Writer
org.fife.io.UnicodeWriter
- All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable
Writes Unicode text to an output stream. If the specified encoding is
UTF-16 or UTF-32, then the text is preceded by the proper Unicode BOM.
If it is any other encoding, this class behaves just like
OutputStreamWriter. This class is here because Java's
OutputStreamWriter apparently doesn't believe in writing
BOMs.
For optimum performance, it is recommended that you wrap all instances of
UnicodeWriter with a java.io.BufferedWriter.
- Version:
- 0.8
- Author:
- Robert Futrell
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringIf this system property evaluates to "true", ignoring case, files written out as UTF-8 will have a BOM written for them. -
Constructor Summary
ConstructorsConstructorDescriptionUnicodeWriter(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(File file, Charset charset) 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(OutputStream out, Charset charset) 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.UnicodeWriter(String fileName, Charset charset) This is a utility constructor since the vast majority of the time, this class will be used to write Unicode files. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this writer.voidflush()Flushes the stream.Returns the encoding being used to write this output stream (i.e., the encoding of the file).static booleanReturns 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.voidWrites a portion of a string.
-
Field Details
-
PROPERTY_WRITE_UTF8_BOM
If this system property evaluates to "true", ignoring case, files written out as UTF-8 will have a BOM written for them. Otherwise, (even if the property is not set), UTF-8 files will not have a BOM written (which is typical, older builds of Windows Notepad are the outlier here).- See Also:
-
-
Constructor Details
-
UnicodeWriter
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.charset- The character set to use.- Throws:
IOException- If an IO exception occurs.- See Also:
-
UnicodeWriter
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.encoding- The encoding to use.- Throws:
IOException- If an IO exception occurs.
-
UnicodeWriter
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.charset- The character set to use.- Throws:
IOException- If an IO exception occurs.- See Also:
-
UnicodeWriter
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.encoding- The encoding to use.- Throws:
IOException- If an IO exception occurs.
-
UnicodeWriter
Creates a new writer.- Parameters:
out- The output stream to write.charset- The character set to use.- Throws:
IOException- If an IO exception occurs.
-
UnicodeWriter
Creates a new writer.- Parameters:
out- The output stream to write.encoding- The encoding to use.- Throws:
IOException- If an IO exception occurs.
-
-
Method Details
-
close
Closes this writer.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classWriter- Throws:
IOException- If an IO exception occurs.
-
flush
Flushes the stream.- Specified by:
flushin interfaceFlushable- Specified by:
flushin classWriter- Throws:
IOException- If an IO exception occurs.
-
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
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:
-
write
Writes 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
Writes a single character.- Overrides:
writein classWriter- Parameters:
c- An integer specifying the character to write.- Throws:
IOException- If an IO error occurs.
-
write
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.
-