public class FileLineReader extends LineNumberReader implements Iterable<String>
FileLineReader instance represents the lines of a file. The
lines may be streamed through an iterator or eturned all at once.
This class encapsulates good practices for resources and adapts
file line reading to for-each loops by implementing the Iterable interface.
The definition of a line is defined by the BufferedReader class's BufferedReader.readLine() method.
The two standard usage patterns are streaming and all-at-once
reading. For streaming, this class implements the Iterable
interface, so for-each loops work:
The iterable implementation reads a line at a time using a buffered reader, so is scalable.File file = ...; FileLineReader lines = new FileLineReader(file,"UTF-8"); for (String line : lines) { processLine(line); } lines.close();
The readLines() method returns the lines all at once
as a collection, and automatically closes all resources used:
The lines are read into the list, so enough memory should be available to hold the entire file.List<String> lines = new FileLines(file,"UTF-8").readLines();
Instances of this class may be used as ordinary line number
readers, too. The LineNumberReader.getLineNumber() method is particularly
useful for error reporting.
| Constructor and Description |
|---|
FileLineReader(File file,
String encoding)
Construct a new file lines iterator from the specified file
using the specified character encoding, assuming no
compression.
|
FileLineReader(File file,
String encoding,
boolean gzipped)
Construct a new file lines iterator from the specified file
using the specified character encoding, uncompressing gzipped
input if the compresison flag is true.
|
| Modifier and Type | Method and Description |
|---|---|
Iterator<String> |
iterator()
Returns an iterator over the remaining lines of the file.
|
static String[] |
readLineArray(File in,
String encoding)
Return the array of lines read from the specified file using
the specified character encoding.
|
List<String> |
readLines()
Returns the list of lines remaining to be read from this line
iterator and closes all resources.
|
static List<String> |
readLines(File in,
String encoding)
Return the list of lines read from the specified file using
the specified character encoding.
|
getLineNumber, mark, read, read, readLine, reset, setLineNumber, skipclose, lines, markSupported, readyclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitforEach, spliteratorpublic FileLineReader(File file, String encoding) throws IOException
Warning: The iterator should be closed using the
BufferedReader.close() method to avoid any dangling file references.
Closing the JVM also closes, so short programs may avoid
closing the streams explicitly.
If the file is not found or the encoding is not supported,
any file-system resources allocated will be released and an
IOException thrown.
file - File from which to read lines.encoding - Character encoding.FileNotFoundException - If the file is not found.UnsupportedEncodingException - If the specified encoding
is not supported.IOExceptionpublic FileLineReader(File file, String encoding, boolean gzipped) throws IOException
Warning: The iterator should be closed using the
BufferedReader.close() method to avoid any dangling file references.
Closing the JVM also closes, so short programs may avoid
closing the streams explicitly.
If the file is not found or the encoding is not supported,
any file-system resources allocated will be released and an
IOException thrown.
file - File from which to read lines.encoding - Character encoding.gzipped - Set to true if file is gzipped.FileNotFoundException - If the file is not found.UnsupportedEncodingException - If the specified encoding
is not supported.IOExceptionpublic Iterator<String> iterator()
LineNumberReader.getLineNumber() from this class will be one greater than it
should be.
There is no concurrent protection for this method, so it should only be used from a single thread.
public List<String> readLines() throws IOException
iterator(), it returns all the
lines read from the file.IOException - If there is an underlying I/O error
reading from the file.public static List<String> readLines(File in, String encoding) throws IOException, UnsupportedEncodingException
in - File whose lines are read.encoding - Character encoding to decode chars in files.UnsupportedEncodingException - If the encoding is not
supported on the JVM.IOException - If there is an underlying I/O error
reading from the file.public static String[] readLineArray(File in, String encoding) throws IOException, UnsupportedEncodingException
in - File whose lines are read.encoding - Character encoding to decode chars in files.UnsupportedEncodingException - If the encoding is not
supported on the JVM.IOException - If there is an underlying I/O error
reading from the file.Copyright © 2019 Alias-i, Inc.. All rights reserved.