Class ReversedFileLineReader

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class ReversedFileLineReader
    extends org.apache.commons.io.input.ReversedLinesFileReader
    This tool reads a file from its end to its start.
    Author:
    Yohann Chastagnier
    • Nested Class Summary

      • Nested classes/interfaces inherited from class org.apache.commons.io.input.ReversedLinesFileReader

        org.apache.commons.io.input.ReversedLinesFileReader.Builder
    • Method Detail

      • readLastLines

        public static List<String> readLastLines​(Path path,
                                                 int lastLines)
                                          throws IOException
        Reads a specified number of last lines of a file represented by a Path instance.
        The result will never contains more than FULL_FILE_NB_LINE_LIMIT lines.
        Last lines are sorted from oldest to the most recent.
        Empty line at end of file is skipped.
        Parameters:
        path - the path where the file data are located.
        lastLines - the number of last lines to read. 0 or negative value to get all the lines of the file. However, the result contains at most FULL_FILE_NB_LINE_LIMIT lines in order to avoid memory problems.
        Returns:
        a list containing the last requested lines. The result contains at most FULL_FILE_NB_LINE_LIMIT lines.
        Throws:
        IOException - if an I/O error occurs.
      • readLastLines

        public static List<String> readLastLines​(Path path,
                                                 int lastLines,
                                                 boolean sortedFromOldestToMostRecent)
                                          throws IOException
        Reads a specified number of last lines of a file represented by a Path instance.
        The result will never contains more than FULL_FILE_NB_LINE_LIMIT lines.
        Empty line at end of file is skipped.
        Parameters:
        path - the path where the file data are located.
        lastLines - the number of last lines to read. 0 or negative value to get all the lines of the file. However, the result contains at most FULL_FILE_NB_LINE_LIMIT lines in order to avoid memory problems.
        sortedFromOldestToMostRecent - true to sort the last lines from oldest to most recent, false to sort in the reverse way.
        Returns:
        a list containing the last requested lines. The result contains at most FULL_FILE_NB_LINE_LIMIT lines.
        Throws:
        IOException - if an I/O error occurs.