public final class LineIterator extends Object implements 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(Reader reader)
Constructs an iterator of the lines for a
Reader . |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the underlying
Reader quietly. |
<E extends Exception> |
foreachRemaining(Throwables.Consumer<? super String,E> action) |
boolean |
hasNext()
Indicates whether the
Reader has more lines. |
String |
next()
Returns the next line in the wrapped
Reader . |
static LineIterator |
of(File file)
Returns an Iterator for the lines in a
File using the default encoding for the VM. |
static LineIterator |
of(File file,
Charset encoding)
Returns an Iterator for the lines in a
File . |
static LineIterator |
of(InputStream input) |
static LineIterator |
of(InputStream input,
Charset encoding)
Returns an Iterator for the lines in an
InputStream , using
the character encoding specified (or default encoding if null). |
static LineIterator |
of(Reader reader) |
void |
remove()
Deprecated.
- UnsupportedOperationException
|
<C extends Collection<T>> |
toCollection(Supplier<? extends C> supplier) |
ImmutableList<T> |
toImmutableList() |
ImmutableSet<T> |
toImmutableSet() |
Set<T> |
toSet() |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEachRemaining
public LineIterator(Reader reader) throws IllegalArgumentException
Reader
.reader
- the Reader
to read from, not nullIllegalArgumentException
- if the reader is nullpublic static LineIterator of(File file)
File
using the default encoding for the VM.
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
null
UncheckedIOException
- in case of an I/O error (file closed)#lineIterator(File, Charset)
public static LineIterator of(File file, 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(InputStream input)
input
- public static LineIterator of(InputStream input, 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 defaultIllegalArgumentException
- if the input is nullUncheckedIOException
- if an I/O error occurs, such as if the encoding is invalidpublic static LineIterator of(Reader reader)
reader
- public boolean hasNext()
Reader
has more lines.
If there is an IOException
then close()
will
be called on this instance.hasNext
in interface Iterator<String>
true
if the Reader has more linesIllegalStateException
- if an IO exception occurspublic String next()
Reader
.next
in interface Iterator<String>
NoSuchElementException
- if there is no line to returnpublic <E extends Exception> void foreachRemaining(Throwables.Consumer<? super String,E> action) throws E extends Exception
E
- action
- E
- the eE extends Exception
public 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 Closeable
close
in interface AutoCloseable
@Deprecated public void remove() throws UnsupportedOperationException
remove
in interface Iterator<T>
UnsupportedOperationException
public Set<T> toSet()
public <C extends Collection<T>> C toCollection(Supplier<? extends C> supplier)
C
- supplier
- public ImmutableList<T> toImmutableList()
public ImmutableSet<T> toImmutableSet()
Copyright © 2020. All rights reserved.