Package org.apache.commons.io.input
Class ReversedLinesFileReader
- java.lang.Object
 - 
- org.apache.commons.io.input.ReversedLinesFileReader
 
 
- 
- All Implemented Interfaces:
 java.io.Closeable,java.lang.AutoCloseable
public class ReversedLinesFileReader extends java.lang.Object implements java.io.CloseableReads 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.- Since:
 - 2.2
 - See Also:
 ReversedLinesFileReader.Builder
 
- 
- 
Nested Class Summary
Nested Classes Modifier and Type Class Description static classReversedLinesFileReader.BuilderBuilds a newReversedLinesFileReader. 
- 
Constructor Summary
Constructors Constructor Description ReversedLinesFileReader(java.io.File file)Deprecated.ReversedLinesFileReader(java.io.File file, int blockSize, java.lang.String charsetName)Deprecated.ReversedLinesFileReader(java.io.File file, int blockSize, java.nio.charset.Charset charset)Deprecated.ReversedLinesFileReader(java.io.File file, java.nio.charset.Charset charset)Deprecated.ReversedLinesFileReader(java.nio.file.Path file, int blockSize, java.lang.String charsetName)Deprecated.ReversedLinesFileReader(java.nio.file.Path file, int blockSize, java.nio.charset.Charset charset)Deprecated.ReversedLinesFileReader(java.nio.file.Path file, java.nio.charset.Charset charset)Deprecated. 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ReversedLinesFileReader.Builderbuilder()Constructs a newReversedLinesFileReader.Builder.voidclose()Closes underlying resources.java.lang.StringreadLine()Returns the lines of the file from bottom to top.java.util.List<java.lang.String>readLines(int lineCount)ReturnslineCountlines of the file from bottom to top.java.lang.StringtoString(int lineCount)Returns the lastlineCountlines of the file. 
 - 
 
- 
- 
Constructor Detail
- 
ReversedLinesFileReader
@Deprecated public ReversedLinesFileReader(java.io.File file) throws java.io.IOExceptionDeprecated.Constructs a ReversedLinesFileReader with default block size of 4KB and the platform's default encoding.- Parameters:
 file- the file to be read- Throws:
 java.io.IOException- if an I/O error occurs.
 
- 
ReversedLinesFileReader
@Deprecated public ReversedLinesFileReader(java.io.File file, java.nio.charset.Charset charset) throws java.io.IOExceptionDeprecated.Constructs a ReversedLinesFileReader with default block size of 4KB and the specified encoding.- Parameters:
 file- the file to be readcharset- the charset to use, null uses the default Charset.- Throws:
 java.io.IOException- if an I/O error occurs.- Since:
 - 2.5
 
 
- 
ReversedLinesFileReader
@Deprecated public ReversedLinesFileReader(java.io.File file, int blockSize, java.nio.charset.Charset charset) throws java.io.IOExceptionDeprecated.Constructs a ReversedLinesFileReader with the given block size and encoding.- Parameters:
 file- the file to be readblockSize- 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:
 java.io.IOException- if an I/O error occurs.- Since:
 - 2.3
 
 
- 
ReversedLinesFileReader
@Deprecated public ReversedLinesFileReader(java.io.File file, int blockSize, java.lang.String charsetName) throws java.io.IOExceptionDeprecated.Constructs a ReversedLinesFileReader with the given block size and encoding.- Parameters:
 file- the file to be readblockSize- 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:
 java.io.IOException- if an I/O error occursjava.nio.charset.UnsupportedCharsetException- if the encoding is not supported
 
- 
ReversedLinesFileReader
@Deprecated public ReversedLinesFileReader(java.nio.file.Path file, java.nio.charset.Charset charset) throws java.io.IOExceptionDeprecated.Constructs a ReversedLinesFileReader with default block size of 4KB and the specified encoding.- Parameters:
 file- the file to be readcharset- the charset to use, null uses the default Charset.- Throws:
 java.io.IOException- if an I/O error occurs.- Since:
 - 2.7
 
 
- 
ReversedLinesFileReader
@Deprecated public ReversedLinesFileReader(java.nio.file.Path file, int blockSize, java.nio.charset.Charset charset) throws java.io.IOExceptionDeprecated.Constructs a ReversedLinesFileReader with the given block size and encoding.- Parameters:
 file- the file to be readblockSize- 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:
 java.io.IOException- if an I/O error occurs.- Since:
 - 2.7
 
 
- 
ReversedLinesFileReader
@Deprecated public ReversedLinesFileReader(java.nio.file.Path file, int blockSize, java.lang.String charsetName) throws java.io.IOExceptionDeprecated.Constructs a ReversedLinesFileReader with the given block size and encoding.- Parameters:
 file- the file to be readblockSize- 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:
 java.io.IOException- if an I/O error occursjava.nio.charset.UnsupportedCharsetException- if the encoding is not supported- Since:
 - 2.7
 
 
 - 
 
- 
Method Detail
- 
builder
public static ReversedLinesFileReader.Builder builder()
Constructs a newReversedLinesFileReader.Builder.- Returns:
 - a new 
ReversedLinesFileReader.Builder. - Since:
 - 2.12.0
 
 
- 
close
public void close() throws java.io.IOExceptionCloses underlying resources.- Specified by:
 closein interfacejava.lang.AutoCloseable- Specified by:
 closein interfacejava.io.Closeable- Throws:
 java.io.IOException- if an I/O error occurs.
 
- 
readLine
public java.lang.String readLine() throws java.io.IOExceptionReturns the lines of the file from bottom to top.- Returns:
 - the next line or null if the start of the file is reached
 - Throws:
 java.io.IOException- if an I/O error occurs.
 
- 
readLines
public java.util.List<java.lang.String> readLines(int lineCount) throws java.io.IOExceptionReturnslineCountlines of the file from bottom to top.If there are less than
lineCountlines 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:
 java.io.IOException- if an I/O error occurs.- Since:
 - 2.8.0
 
 
- 
toString
public java.lang.String toString(int lineCount) throws java.io.IOExceptionReturns the lastlineCountlines of the file.If there are less than
lineCountlines in the file, then that's what you get.- Parameters:
 lineCount- How many lines to read.- Returns:
 - A String.
 - Throws:
 java.io.IOException- if an I/O error occurs.- Since:
 - 2.8.0
 
 
 - 
 
 -