"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Efficiently Retrieve the Last Line of a Large Text File?

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

Published on 2024-11-16
Browse:130

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.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3