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'sOutputStreamWriter
apparently doesn't believe in writing BOMs.For optimum performance, it is recommended that you wrap all instances of
UnicodeWriter
with ajava.io.BufferedWriter
.
-
-
Field Summary
Fields Modifier and Type Field Description static 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.
-
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 void
close()
Closes this writer.void
flush()
Flushes the stream.String
getEncoding()
Returns the encoding being used to write this output stream (i.e., the encoding of the file).static boolean
getWriteUtf8BOM()
Returns whether UTF-8 files should have a BOM in them when written.static void
setWriteUtf8BOM(boolean write)
Sets whether UTF-8 files should have a BOM written in them.void
write(char[] cbuf, int off, int len)
Writes a portion of an array of characters.void
write(int c)
Writes a single character.void
write(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 IOException
Closes this writer.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classWriter
- Throws:
IOException
- If an IO exception occurs.
-
flush
public void flush() throws IOException
Flushes the stream.- Specified by:
flush
in interfaceFlushable
- Specified by:
flush
in 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 IOException
Writes a portion of an array of characters.- Specified by:
write
in 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 IOException
Writes a single character.- Overrides:
write
in 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:
write
in 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.
-
-