public class TextFileReader
extends java.io.InputStreamReader
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
.
InputStreamReader
,
FileInputStream
,
FileReader
Modifier and Type | Field and Description |
---|---|
static java.nio.charset.Charset |
DEFAULT_CHARSET
The default character encoding used by this class.
|
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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(ILineProcessor lineProcessor)
Processes all lines of the underlying text file by reading
each line and sending it to the given line processor.
|
public static final java.nio.charset.Charset DEFAULT_CHARSET
public TextFileReader(java.io.File file, java.nio.charset.Charset charset) throws java.io.FileNotFoundException
file
- the File to read from (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
reading.public TextFileReader(java.io.File file) throws java.io.FileNotFoundException
If you want to have the platform encoding instead, use FileReader
instead.
If you want to have another encoding instead, use TextFileReader(File, Charset)
.
file
- the File to read from (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
reading.public TextFileReader(java.lang.String filename, java.nio.charset.Charset charset) throws java.io.FileNotFoundException
filename
- The name of the file to read from (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
reading.public TextFileReader(java.lang.String filename) throws java.io.FileNotFoundException
If you want to have the platform encoding instead, use FileReader
instead.
If you want to have another encoding instead, use TextFileReader(String, Charset)
.
filename
- The name of the file to read from (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
reading.public int processLines(ILineProcessor lineProcessor)
lineProcessor
- The processor to receive line by line (must not be null).IORuntimeException
- In any case of IO problems.public void closeQuietly()
IOException
that might occur.public void closeUnchecked()
IORuntimeException
wrapping
any IOException
that might occur.IORuntimeException
- A wrapped IOException
.