Home >Java >javaTutorial >How Can I Efficiently Count Lines in a Large Java File?

How Can I Efficiently Count Lines in a Large Java File?

Barbara Streisand
Barbara StreisandOriginal
2024-12-26 09:30:11236browse

How Can I Efficiently Count Lines in a Large Java File?

Counting Lines in a File Efficiently in Java

Problem:

When working with large data files, it is often necessary to determine the number of lines in the file. While it is possible to read the file line by line until the end is reached, this method can be time-consuming and inefficient.

Solution:

A more efficient way to count the number of lines in a file in Java is to use the following code:

public static int countLines(String filename) throws IOException {
    InputStream is = new BufferedInputStream(new FileInputStream(filename));
    try {
        byte[] c = new byte[1024];
        int readChars = is.read(c);
        if (readChars == -1) {
            // bail out if nothing to read
            return 0;
        }
        // make it easy for the optimizer to tune this loop
        int count = 0;
        while (readChars == 1024) {
            for (int i = 0; i < 1024;) {
                if (c[i++] == '\n') {
                    ++count;
                }
            }
            readChars = is.read(c);
        }
        // count remaining characters
        while (readChars != -1) {
            for (int i = 0; i < readChars; ++i) {
                if (c[i] == '\n') {
                    ++count;
                }
            }
            readChars = is.read(c);
        }
        return count == 0 ? 1 : count;
    } finally {
        is.close();
    }
}

This code is significantly faster than the naive approach of reading the file line by line, especially for large files. The use of a buffered input stream and the optimized loop allow for processing the file data efficiently and minimizes unnecessary read operations.

The above is the detailed content of How Can I Efficiently Count Lines in a Large Java 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