Class LineIterator
- All Implemented Interfaces:
Immutable
,Closeable
,AutoCloseable
,Iterator<String>
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 } }
- Since:
- 1.2
- Version:
- $Id: LineIterator.java 1471767 2013-04-24 23:24:19Z sebb $
-
Constructor Summary
ConstructorDescriptionLineIterator
(Reader reader) Constructs an iterator of the lines for aReader
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the underlyingReader
quietly.<E extends Exception>
voidforeachIndexed
(Throwables.IndexedConsumer<? super String, E> action) <E extends Exception>
voidforeachRemaining
(Throwables.Consumer<? super String, E> action) boolean
hasNext()
Indicates whether theReader
has more lines.next()
Returns the next line in the wrappedReader
.static LineIterator
Returns an Iterator for the lines in aFile
using the default encoding for the VM.static LineIterator
Returns an Iterator for the lines in aFile
.static LineIterator
of
(InputStream input) static LineIterator
of
(InputStream input, Charset encoding) Returns an Iterator for the lines in anInputStream
, using the character encoding specified (or default encoding if null).static LineIterator
void
remove()
Deprecated.- UnsupportedOperationException<C extends Collection<T>>
CtoCollection
(Supplier<? extends C> supplier) toSet()
Methods inherited from class com.landawn.abacus.util.ObjIterator
defer, empty, filter, generate, generate, indexed, indexed, just, map, of, of, of, of, of, stream, toArray, toArray, toList
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Iterator
forEachRemaining
-
Constructor Details
-
LineIterator
Constructs an iterator of the lines for aReader
.- Parameters:
reader
- theReader
to read from, not null- Throws:
IllegalArgumentException
- if the reader is null
-
-
Method Details
-
of
Returns an Iterator for the lines in aFile
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 theclose()
orIOUtil.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.
- Parameters:
file
- the file to open for input, must not benull
- Returns:
- an Iterator of the lines in the file, never
null
- Throws:
UncheckedIOException
- in case of an I/O error (file closed)- See Also:
-
#lineIterator(File, Charset)
-
of
Returns an Iterator for the lines in aFile
.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 theclose()
orIOUtil.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.
- Parameters:
file
- the file to open for input, must not benull
encoding
- the encoding to use,null
means platform default- Returns:
- an Iterator of the lines in the file, never
null
- Throws:
UncheckedIOException
- in case of an I/O error (file closed)
-
of
- Parameters:
input
-- Returns:
-
of
Returns an Iterator for the lines in anInputStream
, using the character encoding specified (or default encoding if null).LineIterator
holds a reference to the openInputStream
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 callingclose()
orIOUtil.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); }
- Parameters:
input
- theInputStream
to read from, not nullencoding
- the encoding to use, null means platform default- Returns:
- an Iterator of the lines in the reader, never null
- Throws:
IllegalArgumentException
- if the input is nullUncheckedIOException
- if an I/O error occurs, such as if the encoding is invalid
-
of
- Parameters:
reader
-- Returns:
-
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 interfaceIterator<String>
- Returns:
true
if the Reader has more lines- Throws:
IllegalStateException
- if an IO exception occurs
-
next
Returns the next line in the wrappedReader
.- Specified by:
next
in interfaceIterator<String>
- Returns:
- Throws:
NoSuchElementException
- if there is no line to return
-
foreachRemaining
public <E extends Exception> void foreachRemaining(Throwables.Consumer<? super String, E> action) throws E- Overrides:
foreachRemaining
in classObjIterator<String>
- Type Parameters:
E
-- Parameters:
action
-- Throws:
E
- the e
-
foreachIndexed
public <E extends Exception> void foreachIndexed(Throwables.IndexedConsumer<? super String, E> action) throws E- Overrides:
foreachIndexed
in classObjIterator<String>
- Type Parameters:
E
-- Parameters:
action
-- Throws:
E
- the e
-
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.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
remove
Deprecated.- UnsupportedOperationException- Specified by:
remove
in interfaceIterator<T>
- Throws:
UnsupportedOperationException
-
toSet
-
toCollection
- Type Parameters:
C
-- Parameters:
supplier
-- Returns:
-
toImmutableList
-
toImmutableSet
-