Class LineIterator

java.lang.Object
com.landawn.abacus.util.ObjIterator<String>
com.landawn.abacus.util.LineIterator
All Implemented Interfaces:
Immutable, Closeable, AutoCloseable, Iterator<String>

public final class LineIterator extends ObjIterator<String> implements Closeable
Note: it's copied from Apache Commons IO developed at The Apache Software Foundation (http://www.apache.org/), or under the Apache License 2.0. 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 #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 Details

  • Method Details

    • of

      public static LineIterator of(File file)
      Returns an Iterator for the lines in a 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.

      Parameters:
      file - the file to open for input, must not be null
      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

      public static LineIterator of(File file, Charset encoding)
      Returns an Iterator for the lines in a 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.

      Parameters:
      file - the file to open for input, must not be null
      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

      public static LineIterator of(InputStream input)
      Parameters:
      input -
      Returns:
    • of

      public static LineIterator of(InputStream input, Charset encoding) throws UncheckedIOException
      Returns an Iterator for the lines in an 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);
       }
       
      Parameters:
      input - the InputStream to read from, not null
      encoding - the encoding to use, null means platform default
      Returns:
      an Iterator of the lines in the reader, never null
      Throws:
      UncheckedIOException - if an I/O error occurs, such as if the encoding is invalid
      IllegalArgumentException - if the input is null
    • of

      public static LineIterator of(Reader reader)
      Parameters:
      reader -
      Returns:
    • hasNext

      public boolean hasNext()
      Indicates whether the Reader has more lines. If there is an IOException then close() will be called on this instance.
      Specified by:
      hasNext in interface Iterator<String>
      Returns:
      true if the Reader has more lines
      Throws:
      IllegalStateException - if an IO exception occurs
    • next

      public String next()
      Returns the next line in the wrapped Reader.
      Specified by:
      next in interface Iterator<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 class ObjIterator<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 class ObjIterator<String>
      Type Parameters:
      E -
      Parameters:
      action -
      Throws:
      E - the e
    • close

      public void close()
      Closes the underlying 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.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • remove

      @Deprecated public void remove() throws UnsupportedOperationException
      Deprecated.
      - UnsupportedOperationException
      Specified by:
      remove in interface Iterator<T>
      Throws:
      UnsupportedOperationException
    • toSet

      public Set<String> toSet()
      Returns:
    • toCollection

      public <C extends Collection<T>> C toCollection(Supplier<? extends C> supplier)
      Type Parameters:
      C -
      Parameters:
      supplier -
      Returns:
    • toImmutableList

      public ImmutableList<String> toImmutableList()
      Returns:
    • toImmutableSet

      public ImmutableSet<String> toImmutableSet()
      Returns: