org.pfsw.text
Class TextFileWriter

java.lang.Object
  extended by java.io.Writer
      extended by java.io.OutputStreamWriter
          extended by org.pfsw.text.TextFileWriter
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, java.lang.Appendable
Direct Known Subclasses:
UncheckedTextFileWriter

public class TextFileWriter
extends java.io.OutputStreamWriter

Convenience class for writing text files. In contrary to 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.

See Also:
OutputStreamWriter, FileOutputStream, FileWriter

Field Summary
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.
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
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.
 
Method Summary
 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(org.pfsw.bif.text.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(org.pfsw.bif.text.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...).
 
Methods inherited from class java.io.OutputStreamWriter
close, flush, getEncoding, write, write, write
 
Methods inherited from class java.io.Writer
append, append, append, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CHARSET

public static final java.nio.charset.Charset DEFAULT_CHARSET
The default character encoding used by this class.


DEFAULT_NEWLINE

public static final NewLine DEFAULT_NEWLINE
The default newline definition used by this class.

Constructor Detail

TextFileWriter

public TextFileWriter(java.io.File file,
                      java.nio.charset.Charset charset)
               throws java.io.FileNotFoundException
Creates a new TextFileWriter, given the File to write to with the encoding specified by the given charset.

Parameters:
file - the File to write to (must not be null).
charset - The encoding to be used (must not be null).
Throws:
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.

TextFileWriter

public TextFileWriter(java.io.File file)
               throws java.io.FileNotFoundException
Creates a new TextFileWriter, given the File to write to with the default character encoding (UTF-8).

If you want to have the platform encoding instead, use FileWriter instead.
If you want to have another encoding, use TextFileWriter(File, Charset).

Parameters:
file - the File to write to (must not be null).
Throws:
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.

TextFileWriter

public TextFileWriter(java.lang.String filename,
                      java.nio.charset.Charset charset)
               throws java.io.FileNotFoundException
Creates a new TextFileWriter on the file with the given filename to write to with the encoding specified by the given charset.

Parameters:
filename - The name of the file to write to (must not be null).
charset - The encoding to be used (must not be null).
Throws:
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.

TextFileWriter

public TextFileWriter(java.lang.String filename)
               throws java.io.FileNotFoundException
Creates a new TextFileWriter on the file with the given filename to write to with the default character encoding (UTF-8).

If you want to have the platform encoding instead, use FileWriter instead.
If you want to have another encoding, use TextFileWriter(String, Charset).

Parameters:
filename - The name of the file to write to (must not be null).
Throws:
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.
Method Detail

writef

public TextFileWriter writef(java.lang.String text,
                             java.lang.Object... args)
                      throws java.io.IOException
Allows writing with placeholder replacement as provided by String.format(String, Object...).

Parameters:
text - The text message that might contain placeholders (must not be null).
args - Optional arguments to replace placeholders in the given text.
Returns:
The writer itself to allow fluent API usage.
Throws:
java.io.IOException - In any case of writing problem.

write

public TextFileWriter write(org.pfsw.bif.text.IStringRepresentation object)
                     throws java.io.IOException
Writes the given object's IStringRepresentation.asString().

Parameters:
object - The object of which to write its string representation (must not be null).
Returns:
The writer itself to allow fluent API usage.
Throws:
java.io.IOException - In any case of writing problem.

writeln

public TextFileWriter writeln(java.lang.String text,
                              java.lang.Object... args)
                       throws java.io.IOException
Writes the text with placeholder replacement as provided by String.format(String, Object...). followed by newline character(s).

Parameters:
text - The text message that might contain placeholders (must not be null).
args - Optional arguments to replace placeholders in the given text.
Returns:
The writer itself to allow fluent API usage.
Throws:
java.io.IOException - In any case of writing problem.

writeln

public TextFileWriter writeln(org.pfsw.bif.text.IStringRepresentation object)
                       throws java.io.IOException
Writes the given object's IStringRepresentation.asString() followed by newline character(s).

Parameters:
object - The object of which to write its string representation (must not be null).
Returns:
The writer itself to allow fluent API usage.
Throws:
java.io.IOException - In any case of writing problem.

newLine

public TextFileWriter newLine()
                       throws java.io.IOException
Writes the newline character(s).

Returns:
The writer itself to allow fluent API usage.
Throws:
java.io.IOException - In any case of writing problem.

getNewline

public NewLine getNewline()
Returns the new line definition used by this writer. Use NewLine.asString() to get the actual new line character(s).


setNewline

public TextFileWriter setNewline(NewLine newline)
Sets the new line definition to be used by this writer.

Returns:
The writer itself to allow fluent API usage.

closeQuietly

public void closeQuietly()
Closes this writer and swallows any IOException that might occur.


closeUnchecked

public void closeUnchecked()
Closes this writer and throws an IORuntimeException wrapping any IOException that might occur.

Throws:
org.pfsw.bif.exceptions.IORuntimeException - A wrapped IOException.