org.pfsw.text
Class TextFileReader

java.lang.Object
  extended by java.io.Reader
      extended by java.io.InputStreamReader
          extended by org.pfsw.text.TextFileReader
All Implemented Interfaces:
java.io.Closeable, java.lang.Readable

public class TextFileReader
extends java.io.InputStreamReader

Convenience class for reading text files. In contrary to FileReader the constructors of this class support defining character encoding.

TextFileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream.

See Also:
InputStreamReader, FileInputStream, FileReader

Field Summary
static java.nio.charset.Charset DEFAULT_CHARSET
          The default character encoding used by this class.
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
TextFileReader(java.io.File file)
          Creates a new TextFileReader, given the File to read from with the default character encoding (UTF-8) of this class.
TextFileReader(java.io.File file, java.nio.charset.Charset charset)
          Creates a new TextFileReader, given the File to read from with the encoding specified by the given charset.
TextFileReader(java.lang.String filename)
          Creates a new TextFileReader on the file with the given filename to read from with the default character encoding (UTF-8) of this class.
TextFileReader(java.lang.String filename, java.nio.charset.Charset charset)
          Creates a new TextFileReader on the file with the given filename to read from with the encoding specified by the given charset.
 
Method Summary
 void closeQuietly()
          Closes this reader and swallows any IOException that might occur.
 void closeUnchecked()
          Closes this reader and throws an IORuntimeException wrapping any IOException that might occur.
 int processLines(org.pfsw.bif.text.ILineProcessor lineProcessor)
          Processes all lines of the underlying text file by reading each line and sending it to the given line processor.
 
Methods inherited from class java.io.InputStreamReader
close, getEncoding, read, read, ready
 
Methods inherited from class java.io.Reader
mark, markSupported, read, read, reset, skip
 
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.

Constructor Detail

TextFileReader

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

Parameters:
file - the File to read from (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 reading.

TextFileReader

public TextFileReader(java.io.File file)
               throws java.io.FileNotFoundException
Creates a new TextFileReader, given the File to read from with the default character encoding (UTF-8) of this class.

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

Parameters:
file - the File to read from (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 reading.

TextFileReader

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

Parameters:
filename - The name of the file to read from (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 reading.

TextFileReader

public TextFileReader(java.lang.String filename)
               throws java.io.FileNotFoundException
Creates a new TextFileReader on the file with the given filename to read from with the default character encoding (UTF-8) of this class.

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

Parameters:
filename - The name of the file to read from (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 reading.
Method Detail

processLines

public int processLines(org.pfsw.bif.text.ILineProcessor lineProcessor)
Processes all lines of the underlying text file by reading each line and sending it to the given line processor. If the line processor returns false reading further lines is stopped.

Parameters:
lineProcessor - The processor to receive line by line (must not be null).
Returns:
The number of lines read and processed.
Throws:
org.pfsw.bif.exceptions.IORuntimeException - In any case of IO problems.

closeQuietly

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


closeUnchecked

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

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