Class LineIterator
java.lang.Object
org.eclipse.nebula.widgets.picture.internal.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 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 { it.close(); }
- Since:
- Commons IO 1.2
- Version:
- $Id: LineIterator.java 1022322 2010-10-13 23:20:42Z ggregory $
-
Constructor Summary
Constructors Constructor Description LineIterator(java.io.Reader reader)
Constructs an iterator of the lines for aReader
. -
Method Summary
Modifier and Type Method Description void
close()
Closes the underlyingReader
quietly.static void
closeQuietly(LineIterator iterator)
Closes the iterator, handling null and ignoring exceptions.boolean
hasNext()
Indicates whether theReader
has more lines.protected boolean
isValidLine(java.lang.String line)
Overridable method to validate each line that is returned.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.
-
Constructor Details
-
LineIterator
public LineIterator(java.io.Reader reader) throws java.lang.IllegalArgumentExceptionConstructs an iterator of the lines for aReader
.- Parameters:
reader
- theReader
to read from, not null- Throws:
java.lang.IllegalArgumentException
- if the reader is null
-
-
Method Details
-
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
-
isValidLine
protected boolean isValidLine(java.lang.String line)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
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
-
close
public void close()Closes the underlyingReader
quietly. 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. -
remove
public void remove()Unsupported.- Specified by:
remove
in interfacejava.util.Iterator<java.lang.String>
- Throws:
java.lang.UnsupportedOperationException
- always
-
closeQuietly
Closes the iterator, handling null and ignoring exceptions.- Parameters:
iterator
- the iterator to close
-