Package org.apache.commons.io
Class LineIterator
- java.lang.Object
-
- org.apache.commons.io.LineIterator
-
- All Implemented Interfaces:
Closeable,AutoCloseable,Iterator<String>
public class LineIterator extends Object implements Iterator<String>, Closeable
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
-
-
Constructor Summary
Constructors Constructor Description LineIterator(Reader reader)Constructs an iterator of the lines for aReader.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclose()Closes the underlyingReader.static voidcloseQuietly(LineIterator iterator)Deprecated.As of 2.6 deprecated without replacement.booleanhasNext()Indicates whether theReaderhas more lines.Stringnext()Returns the next line in the wrappedReader.StringnextLine()Returns the next line in the wrappedReader.voidremove()Unsupported.-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Iterator
forEachRemaining
-
-
-
-
Constructor Detail
-
LineIterator
public LineIterator(Reader reader) throws IllegalArgumentException
Constructs an iterator of the lines for aReader.- Parameters:
reader- theReaderto read from, not null- Throws:
IllegalArgumentException- if the reader is null
-
-
Method Detail
-
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
-
next
public String 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
public String 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() throws IOExceptionCloses the underlyingReader. 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.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if closing the underlyingReaderfails.
-
remove
public void remove()
Unsupported.- Specified by:
removein interfaceIterator<String>- Throws:
UnsupportedOperationException- always
-
closeQuietly
@Deprecated public static void closeQuietly(LineIterator iterator)
Deprecated.As of 2.6 deprecated without replacement. Please use the try-with-resources statement or handle suppressed exceptions manually.Closes aLineIteratorquietly.- Parameters:
iterator- The iterator to close, ornull.- See Also:
Throwable.addSuppressed(java.lang.Throwable)
-
-