Class ReversedLinesFileReader

java.lang.Object
org.apache.commons.io.input.ReversedLinesFileReader
All Implemented Interfaces:
Closeable, AutoCloseable

public class ReversedLinesFileReader extends Object implements Closeable
Reads lines in a file reversely (similar to a BufferedReader, but starting at the last line). Useful for e.g. searching in log files.
Since:
2.2
  • Field Details

    • EMPTY_STRING

      private static final String EMPTY_STRING
      See Also:
    • DEFAULT_BLOCK_SIZE

      private static final int DEFAULT_BLOCK_SIZE
      See Also:
    • blockSize

      private final int blockSize
    • encoding

      private final Charset encoding
    • channel

      private final SeekableByteChannel channel
    • totalByteLength

      private final long totalByteLength
    • totalBlockCount

      private final long totalBlockCount
    • newLineSequences

      private final byte[][] newLineSequences
    • avoidNewlineSplitBufferSize

      private final int avoidNewlineSplitBufferSize
    • byteDecrement

      private final int byteDecrement
    • currentFilePart

      private ReversedLinesFileReader.FilePart currentFilePart
    • trailingNewlineOfFileSkipped

      private boolean trailingNewlineOfFileSkipped
  • Constructor Details

    • ReversedLinesFileReader

      @Deprecated public ReversedLinesFileReader(File file) throws IOException
      Deprecated.
      Creates a ReversedLinesFileReader with default block size of 4KB and the platform's default encoding.
      Parameters:
      file - the file to be read
      Throws:
      IOException - if an I/O error occurs
    • ReversedLinesFileReader

      public ReversedLinesFileReader(File file, Charset charset) throws IOException
      Creates a ReversedLinesFileReader with default block size of 4KB and the specified encoding.
      Parameters:
      file - the file to be read
      charset - the charset to use, null uses the default Charset.
      Throws:
      IOException - if an I/O error occurs
      Since:
      2.5
    • ReversedLinesFileReader

      public ReversedLinesFileReader(File file, int blockSize, Charset charset) throws IOException
      Creates a ReversedLinesFileReader with the given block size and encoding.
      Parameters:
      file - the file to be read
      blockSize - size of the internal buffer (for ideal performance this should match with the block size of the underlying file system).
      charset - the encoding of the file, null uses the default Charset.
      Throws:
      IOException - if an I/O error occurs
      Since:
      2.3
    • ReversedLinesFileReader

      public ReversedLinesFileReader(File file, int blockSize, String charsetName) throws IOException
      Creates a ReversedLinesFileReader with the given block size and encoding.
      Parameters:
      file - the file to be read
      blockSize - size of the internal buffer (for ideal performance this should match with the block size of the underlying file system).
      charsetName - the encoding of the file, null uses the default Charset.
      Throws:
      IOException - if an I/O error occurs
      UnsupportedCharsetException - thrown instead of UnsupportedEncodingException in version 2.2 if the encoding is not supported.
    • ReversedLinesFileReader

      public ReversedLinesFileReader(Path file, Charset charset) throws IOException
      Creates a ReversedLinesFileReader with default block size of 4KB and the specified encoding.
      Parameters:
      file - the file to be read
      charset - the charset to use, null uses the default Charset.
      Throws:
      IOException - if an I/O error occurs
      Since:
      2.7
    • ReversedLinesFileReader

      public ReversedLinesFileReader(Path file, int blockSize, Charset charset) throws IOException
      Creates a ReversedLinesFileReader with the given block size and encoding.
      Parameters:
      file - the file to be read
      blockSize - size of the internal buffer (for ideal performance this should match with the block size of the underlying file system).
      charset - the encoding of the file, null uses the default Charset.
      Throws:
      IOException - if an I/O error occurs
      Since:
      2.7
    • ReversedLinesFileReader

      public ReversedLinesFileReader(Path file, int blockSize, String charsetName) throws IOException
      Creates a ReversedLinesFileReader with the given block size and encoding.
      Parameters:
      file - the file to be read
      blockSize - size of the internal buffer (for ideal performance this should match with the block size of the underlying file system).
      charsetName - the encoding of the file, null uses the default Charset.
      Throws:
      IOException - if an I/O error occurs
      UnsupportedCharsetException - thrown instead of UnsupportedEncodingException in version 2.2 if the encoding is not supported.
      Since:
      2.7
  • Method Details

    • close

      public void close() throws IOException
      Closes underlying resources.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - if an I/O error occurs
    • 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
      Since:
      2.8.0
    • 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
      Since:
      2.8.0