Package org.apache.commons.io
Class LineIterator
- java.lang.Object
-
- org.apache.commons.io.LineIterator
-
- All Implemented Interfaces:
java.util.Iterator<java.lang.String>
public class LineIterator extends java.lang.Object implements java.util.Iterator<java.lang.String>An Iterator over the lines in aReader.LineIteratorholds a reference to an openReader. 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 theclose()orcloseQuietly(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 { it.close(); }- Since:
- 1.2
- Version:
- $Id: LineIterator.java 1307461 2012-03-30 15:12:29Z ggregory $
-
-
Field Summary
Fields Modifier and Type Field Description private java.io.BufferedReaderbufferedReaderThe reader that is being read.private java.lang.StringcachedLineThe current line.private booleanfinishedA flag indicating if the iterator has been fully read.
-
Constructor Summary
Constructors Constructor Description LineIterator(java.io.Reader reader)Constructs an iterator of the lines for aReader.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the underlyingReaderquietly.static voidcloseQuietly(LineIterator iterator)Closes the iterator, handling null and ignoring exceptions.booleanhasNext()Indicates whether theReaderhas more lines.protected booleanisValidLine(java.lang.String line)Overridable method to validate each line that is returned.java.lang.Stringnext()Returns the next line in the wrappedReader.java.lang.StringnextLine()Returns the next line in the wrappedReader.voidremove()Unsupported.
-
-
-
Method Detail
-
hasNext
public boolean hasNext()
Indicates whether theReaderhas more lines. If there is anIOExceptionthenclose()will be called on this instance.- Specified by:
hasNextin interfacejava.util.Iterator<java.lang.String>- Returns:
trueif the Reader has more lines- Throws:
java.lang.IllegalStateException- if an IO exception occurs
-
isValidLine
protected boolean isValidLine(java.lang.String line)
Overridable method to validate each line that is returned. This implementation always returns true.- Parameters:
line- the line that is to be validated- Returns:
- true if valid, false to remove from the iterator
-
next
public java.lang.String next()
Returns the next line in the wrappedReader.- Specified by:
nextin interfacejava.util.Iterator<java.lang.String>- Returns:
- the next line from the input
- Throws:
java.util.NoSuchElementException- if there is no line to return
-
nextLine
public java.lang.String nextLine()
Returns the next line in the wrappedReader.- Returns:
- the next line from the input
- Throws:
java.util.NoSuchElementException- if there is no line to return
-
close
public void close()
Closes the underlyingReaderquietly. 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 theReaderremains open. This method can safely be called multiple times.
-
remove
public void remove()
Unsupported.- Specified by:
removein interfacejava.util.Iterator<java.lang.String>- Throws:
java.lang.UnsupportedOperationException- always
-
closeQuietly
public static void closeQuietly(LineIterator iterator)
Closes the iterator, handling null and ignoring exceptions.- Parameters:
iterator- the iterator to close
-
-