Package org.apache.commons.io.input
Class ReversedLinesFileReader
java.lang.Object
org.apache.commons.io.input.ReversedLinesFileReader
- All Implemented Interfaces:
- Closeable,- AutoCloseable,- 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));
 }
 
 - Since:
- 2.2
- See Also:
- 
Nested Class SummaryNested Classes
- 
Constructor SummaryConstructorsConstructorDescriptionReversedLinesFileReader(File file) Deprecated.ReversedLinesFileReader(File file, int blockSize, String charsetName) Deprecated.ReversedLinesFileReader(File file, int blockSize, Charset charset) Deprecated.ReversedLinesFileReader(File file, Charset charset) Deprecated.ReversedLinesFileReader(Path file, int blockSize, String charsetName) Deprecated.ReversedLinesFileReader(Path file, int blockSize, Charset charset) Deprecated.ReversedLinesFileReader(Path file, Charset charset) Deprecated.
- 
Method SummaryModifier and TypeMethodDescriptionbuilder()Constructs a newReversedLinesFileReader.Builder.voidclose()Closes underlying resources.iterator()LikeIterable.iterator().readLine()Returns the lines of the file from bottom to top.readLines(int lineCount) ReturnslineCountlines of the file from bottom to top.toString(int lineCount) Returns the lastlineCountlines of the file.unwrap()Unwraps this instance and returns the underlyingIterable.Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.commons.io.function.IOIterableforEach, spliterator
- 
Constructor Details- 
ReversedLinesFileReaderDeprecated.Constructs a ReversedLinesFileReader with default block size of 4KB and the virtual machine'sdefault charset.- Parameters:
- file- the file to be read
- Throws:
- IOException- if an I/O error occurs.
 
- 
ReversedLinesFileReaderDeprecated.Constructs 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@Deprecated public ReversedLinesFileReader(File file, int blockSize, Charset charset) throws IOException Deprecated.Constructs 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@Deprecated public ReversedLinesFileReader(File file, int blockSize, String charsetName) throws IOException Deprecated.Constructs 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- if the encoding is not supported
 
- 
ReversedLinesFileReaderDeprecated.Constructs 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@Deprecated public ReversedLinesFileReader(Path file, int blockSize, Charset charset) throws IOException Deprecated.Constructs 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@Deprecated public ReversedLinesFileReader(Path file, int blockSize, String charsetName) throws IOException Deprecated.Constructs 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- if the encoding is not supported
- Since:
- 2.7
 
 
- 
- 
Method Details- 
builderConstructs a newReversedLinesFileReader.Builder.- Returns:
- a new ReversedLinesFileReader.Builder.
- Since:
- 2.12.0
 
- 
closeCloses underlying resources.- Specified by:
- closein interface- AutoCloseable
- Specified by:
- closein interface- Closeable
- Throws:
- IOException- if an I/O error occurs.
 
- 
iteratorDescription copied from interface:IOIterableLikeIterable.iterator().- Specified by:
- iteratorin interface- IOIterable<String>
- Returns:
- See delegate.
- See Also:
 
- 
readLineReturns 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.
 
- 
readLinesReturnslineCountlines 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:
- IOException- if an I/O error occurs.
- Since:
- 2.8.0
 
- 
toStringReturns 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:
- IOException- if an I/O error occurs.
- Since:
- 2.8.0
 
- 
unwrapDescription copied from interface:IOIterableUnwraps this instance and returns the underlyingIterable.Implementations may not have anything to unwrap and that behavior is undefined for now. - Specified by:
- unwrapin interface- IOIterable<String>
- Returns:
- the underlying Iterable.
 
 
- 
builder(),ReversedLinesFileReader.Builder, andReversedLinesFileReader.Builder.get()