org.pfsw.text
Class UncheckedTextFileWriter

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

public class UncheckedTextFileWriter
extends TextFileWriter

Convenience class for writing text files with throwing IORuntimeException rather than IOException in its methods that extend Writer functionality.
Sometimes (often with lambda expressions) it is very annoying having to explicitly catch checked exceptions. It often "pollutes" the source code so heavily that the actual program logic is hard to be recognized. In such cases it is often much better to write the code straight forward and put one try-catch for RuntimeException around it on a much higher level.
All Writer methods still throw IOException.

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.

UncheckedTextFileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream.

See Also:
OutputStreamWriter, FileOutputStream, FileWriter, TextFileWriter

Field Summary
 
Fields inherited from class org.pfsw.text.TextFileWriter
DEFAULT_CHARSET, DEFAULT_NEWLINE
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
UncheckedTextFileWriter(java.io.File file)
          Creates a new UncheckedTextFileWriter, given the File to write to with the default character encoding (UTF-8).
UncheckedTextFileWriter(java.io.File file, java.nio.charset.Charset charset)
          Creates a new UncheckedTextFileWriter, given the File to write to with the encoding specified by the given charset.
UncheckedTextFileWriter(java.lang.String filename)
          Creates a new UncheckedTextFileWriter on the file with the given filename to write to with the default character encoding (UTF-8).
UncheckedTextFileWriter(java.lang.String filename, java.nio.charset.Charset charset)
          Creates a new UncheckedTextFileWriter on the file with the given filename to write to with the encoding specified by the given charset.
 
Method Summary
static UncheckedTextFileWriter create(java.io.File file)
          Creates a new UncheckedTextFileWriter, given the File to write to with the default character encoding (UTF-8).
static UncheckedTextFileWriter create(java.io.File file, java.nio.charset.Charset charset)
          Creates a new UncheckedTextFileWriter, given the File to write to with the encoding specified by the given charset.
static UncheckedTextFileWriter create(java.lang.String filename)
          Creates a new UncheckedTextFileWriter on the file with the given filename to write to with the default character encoding (UTF-8).
static UncheckedTextFileWriter create(java.lang.String filename, java.nio.charset.Charset charset)
          Creates a new UncheckedTextFileWriter on the file with the given filename to write to with the encoding specified by the given charset.
 UncheckedTextFileWriter newLine()
          Writes the newline character(s).
 UncheckedTextFileWriter setNewline(NewLine newline)
          Sets the new line definition to be used by this writer.
 UncheckedTextFileWriter write(org.pfsw.bif.text.IStringRepresentation object)
          Writes the given object's IStringRepresentation.asString().
 UncheckedTextFileWriter writef(java.lang.String text, java.lang.Object... args)
          Allows writing with placeholder replacement as provided by String.format(String, Object...).
 UncheckedTextFileWriter writeln(org.pfsw.bif.text.IStringRepresentation object)
          Writes the given object's IStringRepresentation.asString() followed by newline character(s).
 UncheckedTextFileWriter 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 org.pfsw.text.TextFileWriter
closeQuietly, closeUnchecked, getNewline
 
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
 

Constructor Detail

UncheckedTextFileWriter

public UncheckedTextFileWriter(java.io.File file,
                               java.nio.charset.Charset charset)
                        throws java.io.FileNotFoundException
Creates a new UncheckedTextFileWriter, 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.

UncheckedTextFileWriter

public UncheckedTextFileWriter(java.io.File file)
                        throws java.io.FileNotFoundException
Creates a new UncheckedTextFileWriter, 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 UncheckedTextFileWriter(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.

UncheckedTextFileWriter

public UncheckedTextFileWriter(java.lang.String filename,
                               java.nio.charset.Charset charset)
                        throws java.io.FileNotFoundException
Creates a new UncheckedTextFileWriter 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.

UncheckedTextFileWriter

public UncheckedTextFileWriter(java.lang.String filename)
                        throws java.io.FileNotFoundException
Creates a new UncheckedTextFileWriter 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 UncheckedTextFileWriter(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

create

public static UncheckedTextFileWriter create(java.io.File file,
                                             java.nio.charset.Charset charset)
Creates a new UncheckedTextFileWriter, 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:
org.pfsw.bif.exceptions.IORuntimeException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for writing.

create

public static UncheckedTextFileWriter create(java.io.File file)
Creates a new UncheckedTextFileWriter, 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 create(File, Charset).

Parameters:
file - the File to write to (must not be null).
Throws:
org.pfsw.bif.exceptions.IORuntimeException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for writing.

create

public static UncheckedTextFileWriter create(java.lang.String filename,
                                             java.nio.charset.Charset charset)
Creates a new UncheckedTextFileWriter 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:
org.pfsw.bif.exceptions.IORuntimeException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for writing.

create

public static UncheckedTextFileWriter create(java.lang.String filename)
Creates a new UncheckedTextFileWriter 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 create(String, Charset).

Parameters:
filename - The name of the file to write to (must not be null).
Throws:
org.pfsw.bif.exceptions.IORuntimeException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for writing.

writef

public UncheckedTextFileWriter writef(java.lang.String text,
                                      java.lang.Object... args)
Allows writing with placeholder replacement as provided by String.format(String, Object...). Use this method instead if Writer.write(String) if IOException checking should be avoided.

Overrides:
writef in class TextFileWriter
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:
org.pfsw.bif.exceptions.IORuntimeException - In any case of writing problem.

write

public UncheckedTextFileWriter write(org.pfsw.bif.text.IStringRepresentation object)
Writes the given object's IStringRepresentation.asString().

Overrides:
write in class TextFileWriter
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:
org.pfsw.bif.exceptions.IORuntimeException - In any case of writing problem.

writeln

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

Overrides:
writeln in class TextFileWriter
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:
org.pfsw.bif.exceptions.IORuntimeException - In any case of writing problem.

writeln

public UncheckedTextFileWriter writeln(org.pfsw.bif.text.IStringRepresentation object)
Writes the given object's IStringRepresentation.asString() followed by newline character(s).

Overrides:
writeln in class TextFileWriter
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:
org.pfsw.bif.exceptions.IORuntimeException - In any case of writing problem.

newLine

public UncheckedTextFileWriter newLine()
Writes the newline character(s).

Overrides:
newLine in class TextFileWriter
Returns:
The writer itself to allow fluent API usage.
Throws:
org.pfsw.bif.exceptions.IORuntimeException - In any case of writing problem.

setNewline

public UncheckedTextFileWriter setNewline(NewLine newline)
Description copied from class: TextFileWriter
Sets the new line definition to be used by this writer.

Overrides:
setNewline in class TextFileWriter
Returns:
The writer itself to allow fluent API usage.