public class LineIterator
extends java.lang.Object
implements java.util.Iterator<java.lang.String>, java.io.Closeable
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 { it.close(); }
Constructor and Description |
---|
LineIterator(java.io.Reader reader)
Constructs an iterator of the lines for a
Reader . |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the underlying
Reader . |
static void |
closeQuietly(LineIterator iterator)
Deprecated.
As of 2.6 removed without replacement. Please use the try-with-resources statement or handle
suppressed exceptions manually.
|
boolean |
hasNext()
Indicates whether the
Reader has more lines. |
java.lang.String |
next()
Returns the next line in the wrapped
Reader . |
java.lang.String |
nextLine()
Returns the next line in the wrapped
Reader . |
void |
remove()
Unsupported.
|
public LineIterator(java.io.Reader reader) throws java.lang.IllegalArgumentException
Reader
.reader
- the Reader
to read from, not nulljava.lang.IllegalArgumentException
- if the reader is nullpublic boolean hasNext()
Reader
has more lines.
If there is an IOException
then close()
will
be called on this instance.hasNext
in interface java.util.Iterator<java.lang.String>
true
if the Reader has more linesjava.lang.IllegalStateException
- if an IO exception occurspublic java.lang.String next()
Reader
.next
in interface java.util.Iterator<java.lang.String>
java.util.NoSuchElementException
- if there is no line to returnpublic java.lang.String nextLine()
Reader
.java.util.NoSuchElementException
- if there is no line to returnpublic void close() throws java.io.IOException
Reader
.
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.close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
- if closing the underlying Reader
fails.public void remove()
remove
in interface java.util.Iterator<java.lang.String>
java.lang.UnsupportedOperationException
- always@Deprecated public static void closeQuietly(LineIterator iterator)
LineIterator
quietly.iterator
- The iterator to close, or null
.Throwable.addSuppressed(java.lang.Throwable)
"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"