org.apache.commons.io

## Class LineIterator

- java.lang.Object
- - org.apache.commons.io.LineIterator

- All Implemented Interfaces:
    Closeable, AutoCloseable, Iterator<String>

      public class LineIterator
      extends Object
      implements Iterator<String>, Closeable

  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 `closeQuietly(LineIterator)` method on the iterator.

  The recommended usage pattern is:

       LineIterator it = FileUtils.lineIterator(file, StandardCharsets.UTF_8.name());
       try {
         while (it.hasNext()) {
           String line = it.nextLine();
           // do something with line
         }
       } finally {
         it.close();
       }

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `LineIterator(Reader reader)` Constructs an iterator of the lines for a `Reader`.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()` Closes the underlying `Reader`.
    `boolean` | `hasNext()` Indicates whether the `Reader` has more lines.
    `protected boolean` | `isValidLine(String line)` Overridable method to validate each line that is returned.
    `String` | `next()` Returns the next line in the wrapped `Reader`.
    `void` | `remove()` Unsupported.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface java.util.Iterator

      `forEachRemaining`

- - ### Constructor Detail

    - #### LineIterator

          public LineIterator(Reader reader)

      Constructs an iterator of the lines for a `Reader`.
      Parameters:
        `reader` - the `Reader` to read from, not null
      Throws:
        `NullPointerException` - if the reader is null

  - ### Method Detail

    - #### close

          public void close()
                     throws IOException

      Closes the underlying `Reader`. 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 `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Throws:
        `IOException` - if closing the underlying `Reader` fails.

    - #### 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

    - #### isValidLine

          protected boolean isValidLine(String line)

      Overridable method to validate each line that is returned. This implementation always returns true.
      Parameters:
        `line` - the line that is to be validated
      Returns:
        true if valid, false to remove from the iterator

    - #### next

          public String next()

      Returns the next line in the wrapped `Reader`.
      Specified by:
        `next` in interface `Iterator<String>`
      Returns:
        the next line from the input
      Throws:
        `NoSuchElementException` - if there is no line to return

    - #### remove

          public void remove()

      Unsupported.
      Specified by:
        `remove` in interface `Iterator<String>`
      Throws:
        `UnsupportedOperationException` - always