public final 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 #closeAllQuietly(LineIterator)
method on the iterator.
The recommended usage pattern is:
try(LineIterator it = FileUtils.lineIterator(file, "UTF-8")) { while (it.hasNext()) { String line = it.nextLine(); // do something with line } }
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 quietly. |
boolean |
hasNext()
Indicates whether the
Reader has more lines. |
java.lang.String |
next()
Returns the next line in the wrapped
Reader . |
static java.util.List<LineIterator> |
of(java.util.Collection<? extends java.io.Reader> readers) |
static LineIterator |
of(java.io.File file)
Returns an Iterator for the lines in a
File using the default encoding for the VM. |
static LineIterator |
of(java.io.File file,
java.nio.charset.Charset encoding)
Returns an Iterator for the lines in a
File . |
static LineIterator |
of(java.io.InputStream input) |
static LineIterator |
of(java.io.InputStream input,
java.nio.charset.Charset encoding)
Returns an Iterator for the lines in an
InputStream , using
the character encoding specified (or default encoding if null). |
static LineIterator |
of(java.io.Reader reader) |
void |
remove()
Deprecated.
|
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 static LineIterator of(java.io.File file)
File
using the default encoding for the VM.file
- the file to open for input, must not be null
null
UncheckedIOException
- in case of an I/O error (file closed)#lineIterator(File, Charset)
public static LineIterator of(java.io.File file, java.nio.charset.Charset encoding)
File
.
This method opens an InputStream
for the file.
When you have finished with the iterator you should close the stream
to free internal resources. This can be done by calling the
close()
or
IOUtil#closeQuietly(LineIterator)
method.
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 { closeQuietly(iterator); }
If an exception occurs during the creation of the iterator, the underlying stream is closed.
file
- the file to open for input, must not be null
encoding
- the encoding to use, null
means platform defaultnull
UncheckedIOException
- in case of an I/O error (file closed)public static LineIterator of(java.io.InputStream input)
public static LineIterator of(java.io.InputStream input, java.nio.charset.Charset encoding)
InputStream
, using
the character encoding specified (or default encoding if null).
LineIterator
holds a reference to the open
InputStream
specified here. When you have finished with
the iterator you should close the stream to free internal resources.
This can be done by closing the stream directly, or by calling
close()
or IOUtil#closeQuietly(LineIterator)
.
The recommended usage pattern is:
try { LineIterator it = lineIterator(stream, charset); while (it.hasNext()) { String line = it.nextLine(); /// do something with line } } finally { closeQuietly(stream); }
input
- the InputStream
to read from, not nullencoding
- the encoding to use, null means platform defaultjava.lang.IllegalArgumentException
- if the input is nullUncheckedIOException
- if an I/O error occurs, such as if the encoding is invalidpublic static LineIterator of(java.io.Reader reader)
public static java.util.List<LineIterator> of(java.util.Collection<? extends java.io.Reader> readers)
public 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 void close()
Reader
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 the Reader
remains open.
This method can safely be called multiple times.close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
@Deprecated public void remove()
remove
in interface java.util.Iterator<java.lang.String>
java.lang.UnsupportedOperationException
- always