@CleanupObligation public class LinesStream extends Object implements Closeable, Iterable<String>
Although LinesStream
implements Iterable
, it
is intended to be first used to initialize a resource in a try-with-resources
statement and then iterated, as in:
try (LinesStream stream = new LinesStream(...)) { for (String line : stream) { ... } }This pattern ensures that the underlying file handle is closed properly.
Like DirectoryStream
, LinesStream
supports
creating at most one Iterator
. Invoking iterator()
to
obtain a second or subsequent Iterator
throws
IllegalStateException
.
Constructor and Description |
---|
LinesStream(Path path)
Opens the text file at
path for reading using charset
StandardCharsets.UTF_8 . |
Modifier and Type | Method and Description |
---|---|
void |
close() |
Iterator<String> |
iterator() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEach, spliterator
public LinesStream(@NonNull Path path) throws IOException
path
for reading using charset
StandardCharsets.UTF_8
.path
- Path to the file to open for reading.IOException
- if the file at path
cannot be opened for
reading.@DischargesObligation public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
Copyright © 2004–2021. All rights reserved.