public class TextFileWriter
extends java.io.OutputStreamWriter
FileWriter
the constructors of this class support defining
character encoding.
Furthermore it allows specifying the new line character(s) to be used.
TextFileWriter
is meant for writing streams of characters.
For writing streams of raw bytes, consider using a FileOutputStream
.
OutputStreamWriter
,
FileOutputStream
,
FileWriter
Modifier and Type | Field and Description |
---|---|
static java.nio.charset.Charset |
DEFAULT_CHARSET
The default character encoding used by this class.
|
static NewLine |
DEFAULT_NEWLINE
The default newline definition used by this class.
|
Constructor and Description |
---|
TextFileWriter(java.io.File file)
Creates a new TextFileWriter, given the File
to write to with the default character encoding (UTF-8).
|
TextFileWriter(java.io.File file,
java.nio.charset.Charset charset)
Creates a new TextFileWriter, given the File
to write to with the encoding specified by the given charset.
|
TextFileWriter(java.lang.String filename)
Creates a new TextFileWriter on the file with the given filename
to write to with the default character encoding (UTF-8).
|
TextFileWriter(java.lang.String filename,
java.nio.charset.Charset charset)
Creates a new TextFileWriter on the file with the given filename
to write to with the encoding specified by the given charset.
|
Modifier and Type | Method and Description |
---|---|
void |
closeQuietly()
Closes this writer and swallows any
IOException that might occur. |
void |
closeUnchecked()
Closes this writer and throws an
IORuntimeException wrapping
any IOException that might occur. |
NewLine |
getNewline()
Returns the new line definition used by this writer.
|
TextFileWriter |
newLine()
Writes the newline character(s).
|
TextFileWriter |
setNewline(NewLine newline)
Sets the new line definition to be used by this writer.
|
TextFileWriter |
write(IStringRepresentation object)
Writes the given object's
IStringRepresentation.asString() . |
TextFileWriter |
writef(java.lang.String text,
java.lang.Object... args)
Allows writing with placeholder replacement as provided by
String.format(String, Object...) . |
TextFileWriter |
writeln(IStringRepresentation object)
Writes the given object's
IStringRepresentation.asString() followed
by newline character(s). |
TextFileWriter |
writeln(java.lang.String text,
java.lang.Object... args)
Writes the text with placeholder replacement as provided by
String.format(String, Object...) . |
public static final java.nio.charset.Charset DEFAULT_CHARSET
public static final NewLine DEFAULT_NEWLINE
public TextFileWriter(java.io.File file, java.nio.charset.Charset charset) throws java.io.FileNotFoundException
file
- the File to write to (must not be null).charset
- The encoding to be used (must not be null).java.io.FileNotFoundException
- if the file does not exist,
is a directory rather than a regular file,
or for some other reason cannot be opened for
writing.public TextFileWriter(java.io.File file) throws java.io.FileNotFoundException
If you want to have the platform encoding instead, use FileWriter
instead.
If you want to have another encoding, use TextFileWriter(File, Charset)
.
file
- the File to write to (must not be null).java.io.FileNotFoundException
- if the file does not exist,
is a directory rather than a regular file,
or for some other reason cannot be opened for
writing.public TextFileWriter(java.lang.String filename, java.nio.charset.Charset charset) throws java.io.FileNotFoundException
filename
- The name of the file to write to (must not be null).charset
- The encoding to be used (must not be null).java.io.FileNotFoundException
- if the file does not exist,
is a directory rather than a regular file,
or for some other reason cannot be opened for
writing.public TextFileWriter(java.lang.String filename) throws java.io.FileNotFoundException
If you want to have the platform encoding instead, use FileWriter
instead.
If you want to have another encoding, use TextFileWriter(String, Charset)
.
filename
- The name of the file to write to (must not be null).java.io.FileNotFoundException
- if the file does not exist,
is a directory rather than a regular file,
or for some other reason cannot be opened for
writing.public TextFileWriter writef(java.lang.String text, java.lang.Object... args) throws java.io.IOException
String.format(String, Object...)
.text
- The text message that might contain placeholders (must not be null).args
- Optional arguments to replace placeholders in the given text.java.io.IOException
- In any case of writing problem.public TextFileWriter write(IStringRepresentation object) throws java.io.IOException
IStringRepresentation.asString()
.object
- The object of which to write its string representation (must not be null).java.io.IOException
- In any case of writing problem.public TextFileWriter writeln(java.lang.String text, java.lang.Object... args) throws java.io.IOException
String.format(String, Object...)
.
followed by newline character(s).text
- The text message that might contain placeholders (must not be null).args
- Optional arguments to replace placeholders in the given text.java.io.IOException
- In any case of writing problem.public TextFileWriter writeln(IStringRepresentation object) throws java.io.IOException
IStringRepresentation.asString()
followed
by newline character(s).object
- The object of which to write its string representation (must not be null).java.io.IOException
- In any case of writing problem.public TextFileWriter newLine() throws java.io.IOException
java.io.IOException
- In any case of writing problem.public NewLine getNewline()
NewLine.asString()
to get the actual new line character(s).public TextFileWriter setNewline(NewLine newline)
public void closeQuietly()
IOException
that might occur.public void closeUnchecked()
IORuntimeException
wrapping
any IOException
that might occur.IORuntimeException
- A wrapped IOException
.