Class LineIterator
java.lang.Object
fr.opensagres.xdocreport.core.io.internal.LineIterator
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 );
}
- Since:
- Commons IO 1.2
- Version:
- $Id: LineIterator.java 437567 2006-08-28 06:39:07Z bayard $
- Author:
- Niall Pemberton, Stephen Colebourne, Sandy McArthur
-
Constructor Summary
ConstructorsConstructorDescriptionLineIterator(Reader reader) Constructs an iterator of the lines for aReader. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the underlyingReaderquietly.static voidcloseQuietly(LineIterator iterator) Closes the iterator, handling null and ignoring exceptions.booleanhasNext()Indicates whether theReaderhas more lines.protected booleanisValidLine(String line) Overridable method to validate each line that is returned.next()Returns the next line in the wrappedReader.nextLine()Returns the next line in the wrappedReader.voidremove()Unsupported.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Iterator
forEachRemaining
-
Constructor Details
-
LineIterator
Constructs an iterator of the lines for aReader.- Parameters:
reader- theReaderto read from, not null- Throws:
IllegalArgumentException- if the reader is null
-
-
Method Details
-
hasNext
public boolean hasNext()Indicates whether theReaderhas more lines. If there is anIOExceptionthenclose()will be called on this instance.- Specified by:
hasNextin interfaceIterator<String>- Returns:
trueif the Reader has more lines- Throws:
IllegalStateException- if an IO exception occurs
-
isValidLine
Overridable method to validate each line that is returned.- Parameters:
line- the line that is to be validated- Returns:
- true if valid, false to remove from the iterator
-
next
Returns the next line in the wrappedReader.- Specified by:
nextin interfaceIterator<String>- Returns:
- the next line from the input
- Throws:
NoSuchElementException- if there is no line to return
-
nextLine
Returns the next line in the wrappedReader.- Returns:
- the next line from the input
- Throws:
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 interfaceIterator<String>- Throws:
UnsupportedOperationException- always
-
closeQuietly
Closes the iterator, handling null and ignoring exceptions.- Parameters:
iterator- the iterator to close
-