FileDocCategorySizeDatePackage
LineIterator.javaAPI DocAndroid 1.5 API6100Wed May 06 22:42:46 BST 2009org.apache.commons.io

LineIterator

public class LineIterator extends 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 {@link #close()} or {@link #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);
}
author
Niall Pemberton
author
Stephen Colebourne
author
Sandy McArthur
version
$Id: LineIterator.java 437567 2006-08-28 06:39:07Z bayard $
since
Commons IO 1.2

Fields Summary
private final 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.
Constructors Summary
public LineIterator(Reader reader)
Constructs an iterator of the lines for a Reader.

param
reader the Reader to read from, not null
throws
IllegalArgumentException if the reader is null


                                  
          
        if (reader == null) {
            throw new IllegalArgumentException("Reader must not be null");
        }
        if (reader instanceof BufferedReader) {
            bufferedReader = (BufferedReader) reader;
        } else {
            bufferedReader = new BufferedReader(reader);
        }
    
Methods Summary
public voidclose()
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.

        finished = true;
        IOUtils.closeQuietly(bufferedReader);
        cachedLine = null;
    
public static voidcloseQuietly(org.apache.commons.io.LineIterator iterator)
Closes the iterator, handling null and ignoring exceptions.

param
iterator the iterator to close

        if (iterator != null) {
            iterator.close();
        }
    
public booleanhasNext()
Indicates whether the Reader has more lines. If there is an IOException then {@link #close()} will be called on this instance.

return
true if the Reader has more lines
throws
IllegalStateException if an IO exception occurs

        if (cachedLine != null) {
            return true;
        } else if (finished) {
            return false;
        } else {
            try {
                while (true) {
                    String line = bufferedReader.readLine();
                    if (line == null) {
                        finished = true;
                        return false;
                    } else if (isValidLine(line)) {
                        cachedLine = line;
                        return true;
                    }
                }
            } catch(IOException ioe) {
                close();
                throw new IllegalStateException(ioe.toString());
            }
        }
    
protected booleanisValidLine(java.lang.String line)
Overridable method to validate each line that is returned.

param
line the line that is to be validated
return
true if valid, false to remove from the iterator

        return true;
    
public java.lang.Objectnext()
Returns the next line in the wrapped Reader.

return
the next line from the input
throws
NoSuchElementException if there is no line to return

        return nextLine();
    
public java.lang.StringnextLine()
Returns the next line in the wrapped Reader.

return
the next line from the input
throws
NoSuchElementException if there is no line to return

        if (!hasNext()) {
            throw new NoSuchElementException("No more lines");
        }
        String currentLine = cachedLine;
        cachedLine = null;
        return currentLine;        
    
public voidremove()
Unsupported.

throws
UnsupportedOperationException always

        throw new UnsupportedOperationException("Remove unsupported on LineIterator");