Home > Java > javaTutorial > 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
Release: 2024-11-07 19:10:03
Original
741 people have browsed it

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);
    }
    // ...
}
Copy after login

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);
    }
    // ...
}
Copy after login

Invoke and Use:

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template