Package org.apache.commons.io
Class LineIterator
- java.lang.Object
-
- org.apache.commons.io.LineIterator
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.util.Iterator<java.lang.String>
public class LineIterator extends java.lang.Object implements java.util.Iterator<java.lang.String>, java.io.Closeable
An Iterator over the lines in aReader
.LineIterator
holds 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, StandardCharsets.UTF_8.name()); try { while (it.hasNext()) { String line = it.nextLine(); // do something with line } } finally { it.close(); }
- Since:
- 1.2
-
-
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 Deprecated Methods Modifier and Type Method Description void
close()
Closes the underlyingReader
.static void
closeQuietly(LineIterator iterator)
Deprecated.As of 2.6 deprecated without replacement.boolean
hasNext()
Indicates whether theReader
has more lines.java.lang.String
next()
Returns the next line in the wrappedReader
.java.lang.String
nextLine()
Returns the next line in the wrappedReader
.void
remove()
Unsupported.
-
-
-
Method Detail
-
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 aLineIterator
quietly.- Parameters:
iterator
- The iterator to close, ornull
.- See Also:
Throwable.addSuppressed(Throwable)
-
close
public void close() throws java.io.IOException
Closes 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 theReader
remains open. This method can safely be called multiple times.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
- if closing the underlyingReader
fails.
-
hasNext
public boolean hasNext()
Indicates whether theReader
has more lines. If there is anIOException
thenclose()
will be called on this instance.- Specified by:
hasNext
in interfacejava.util.Iterator<java.lang.String>
- Returns:
true
if the Reader has more lines- Throws:
java.lang.IllegalStateException
- if an IO exception occurs
-
next
public java.lang.String next()
Returns the next line in the wrappedReader
.- Specified by:
next
in 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
-
remove
public void remove()
Unsupported.- Specified by:
remove
in interfacejava.util.Iterator<java.lang.String>
- Throws:
java.lang.UnsupportedOperationException
- always
-
-