org.apache.commons.io.input

## Class ReversedLinesFileReader

- java.lang.Object
- - org.apache.commons.io.input.ReversedLinesFileReader

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

      public class ReversedLinesFileReader
      extends Object
      implements Closeable, IOIterable<String>

  Reads lines in a file reversely (similar to a BufferedReader, but starting at the last line). Useful for e.g. searching in log files.

  To build an instance, use `ReversedLinesFileReader.Builder`.

  For example:

       try (ReversedLinesFileReader reader = ReversedLinesFileReader.builder()
         .setPath(path)
         .setBufferSize(4096)
         .setCharset(StandardCharsets.UTF_8)
         .get()) {
            reader.forEach(line -> System.out.println(line));
       }

  See Also:
    `ReversedLinesFileReader.Builder`

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `ReversedLinesFileReader.Builder` Builds a new `ReversedLinesFileReader`.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static ReversedLinesFileReader.Builder` | `builder()` Constructs a new `ReversedLinesFileReader.Builder`.
    `void` | `close()` Closes underlying resources.
    `IOIterator<String>` | `iterator()` Like `Iterable.iterator()`.
    `String` | `readLine()` Returns the lines of the file from bottom to top.
    `List<String>` | `readLines(int lineCount)` Returns `lineCount` lines of the file from bottom to top.
    `String` | `toString(int lineCount)` Returns the last `lineCount` lines of the file.
    `Iterable<String>` | `unwrap()` Unwraps this instance and returns the underlying `Iterable`.

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

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

    - ### Methods inherited from interface org.apache.commons.io.function.IOIterable

      `forEach, spliterator`

- - ### Method Detail

    - #### builder

          public static ReversedLinesFileReader.Builder builder()

      Constructs a new `ReversedLinesFileReader.Builder`.
      Returns:
        a new `ReversedLinesFileReader.Builder`.

    - #### close

          public void close()
                     throws IOException

      Closes underlying resources.
      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Throws:
        `IOException` - if an I/O error occurs.

    - #### iterator

          public IOIterator<String> iterator()

      Description copied from interface: `IOIterable`
      Like `Iterable.iterator()`.
      Specified by:
        `iterator` in interface `IOIterable<String>`
      Returns:
        See `delegate`.
      See Also:
        `Iterable.iterator()`

    - #### readLine

          public String readLine()
                          throws IOException

      Returns the lines of the file from bottom to top.
      Returns:
        the next line or null if the start of the file is reached
      Throws:
        `IOException` - if an I/O error occurs.

    - #### readLines

          public List<String> readLines(int lineCount)
                                 throws IOException

      Returns `lineCount` lines of the file from bottom to top.

      If there are less than `lineCount` lines in the file, then that's what you get.

      Note: You can easily flip the result with `Collections.reverse(List)`.

      Parameters:
        `lineCount` - How many lines to read.
      Returns:
        A new list
      Throws:
        `IOException` - if an I/O error occurs.

    - #### toString

          public String toString(int lineCount)
                          throws IOException

      Returns the last `lineCount` lines of the file.

      If there are less than `lineCount` lines in the file, then that's what you get.

      Parameters:
        `lineCount` - How many lines to read.
      Returns:
        A String.
      Throws:
        `IOException` - if an I/O error occurs.

    - #### unwrap

          public Iterable<String> unwrap()

      Description copied from interface: `IOIterable`
      Unwraps this instance and returns the underlying `Iterable`.

      Implementations may not have anything to unwrap and that behavior is undefined for now.

      Specified by:
        `unwrap` in interface `IOIterable<String>`
      Returns:
        the underlying Iterable.