Home  >  Article  >  Java  >  How to Efficiently Retrieve the Last Line of a Large Text File?

How to Efficiently Retrieve the Last Line of a Large Text File?

Susan Sarandon
Susan SarandonOriginal
2024-11-07 19:10:03662browse

How to Efficiently Retrieve the Last Line of a Large Text File?

Retrieving the Last Line of a Large Text File with Efficiency

When handling massive text files, retrieving the final line efficiently is crucial. Random access can be time-consuming for large files, making it impractical. Instead, consider these solutions:

Return the Last Non-Blank Line:

The tail function below quickly retrieves the last non-blank line of a file without loading or traversing the entire file. It seeks to the end of the file and reads characters backward until it encounters a line break, then constructs and returns the last line.

public String tail(File file) {
    // ...
    for (long filePointer = fileLength; filePointer != -1; filePointer--) {
        // ...
        sb.append((char) readByte);
    }
    // ...
}

Return the Last N Lines:

If you need the last N lines instead, use the tail2 function. It employs a similar technique, but it counts line breaks to determine the desired lines before reversing and constructing the output.

public String tail2(File file, int lines) {
    // ...
    for (long filePointer = fileLength; filePointer != -1; filePointer--) {
        line = line + 1;
        sb.append((char) readByte);
    }
    // ...
}

Invoke and Use:

File file = new File("D:\stuff\huge.log");
System.out.println(tail(file));
System.out.println(tail2(file, 10));

Unicode Considerations:

The implementation uses character reversal to reconstruct the lines. While generally effective, this can cause Unicode reversal problems with characters like accents and hats. Therefore, thorough testing is essential before using this approach across various languages.

The above is the detailed content of How to Efficiently Retrieve the Last Line of a Large Text File?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn