org.apache.commons.io
Class LineIterator
java.lang.Object
org.apache.commons.io.LineIterator
- Iterator
public class LineIterator
extends java.lang.Object
implements Iterator
An Iterator over the lines in a
Reader
.
LineIterator
holds a reference to an open
Reader
.
When you have finished with the iterator you should close the reader
to free internal resources. This can be done by closing the reader directly,
or by calling the
close()
or
closeQuietly(LineIterator)
method on the iterator.
The recommended usage pattern is:
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
try {
while (it.hasNext()) {
String line = it.nextLine();
/// do something with line
}
} finally {
LineIterator.closeQuietly(iterator);
}
$Id: LineIterator.java 437567 2006-08-28 06:39:07Z bayard $- Niall Pemberton
- Stephen Colebourne
- Sandy McArthur
private BufferedReader | bufferedReader - The reader that is being read.
|
private String | cachedLine - The current line.
|
private boolean | finished - A flag indicating if the iterator has been fully read.
|
LineIterator(Reader reader) - Constructs an iterator of the lines for a
Reader .
|
void | close() - Closes the underlying
Reader quietly.
|
static void | closeQuietly(LineIterator iterator) - Closes the iterator, handling null and ignoring exceptions.
|
boolean | hasNext() - Indicates whether the
Reader has more lines.
|
protected boolean | isValidLine(String line) - Overridable method to validate each line that is returned.
|
Object | next() - Returns the next line in the wrapped
Reader .
|
String | nextLine() - Returns the next line in the wrapped
Reader .
|
void | remove() - Unsupported.
|
bufferedReader
private final BufferedReader bufferedReader
The reader that is being read.
cachedLine
private String cachedLine
The current line.
finished
private boolean finished
A flag indicating if the iterator has been fully read.
LineIterator
public LineIterator(Reader reader)
throws IllegalArgumentException
Constructs an iterator of the lines for a Reader
.
reader
- the Reader
to read from, not null
close
public void close()
Closes the underlying Reader
quietly.
This method is useful if you only want to process the first few
lines of a larger file. If you do not close the iterator
then the Reader
remains open.
This method can safely be called multiple times.
closeQuietly
public static void closeQuietly(LineIterator iterator)
Closes the iterator, handling null and ignoring exceptions.
iterator
- the iterator to close
hasNext
public boolean hasNext()
Indicates whether the
Reader
has more lines.
If there is an
IOException
then
close()
will
be called on this instance.
true
if the Reader has more lines
isValidLine
protected boolean isValidLine(String line)
Overridable method to validate each line that is returned.
line
- the line that is to be validated
- true if valid, false to remove from the iterator
next
public Object next()
Returns the next line in the wrapped Reader
.
- the next line from the input
nextLine
public String nextLine()
Returns the next line in the wrapped Reader
.
- the next line from the input
remove
public void remove()
Unsupported.
Copyright (c) 2002-2009 Apache Software Foundation