API for simple File (line count) functions in Java

眉间皱痕 提交于 2019-12-09 00:54:32

问题


Hi : Given an arbitrary file (java), I want to count the lines.

This is easy enough, for example, using Apache's FileUtils.readLines(...) method...

However, for large files, reading a whole file in place is ludicrous (i.e. just to count lines).

One home-grown option : Create BufferedReader or use the FileUtils.lineIterator function, and count the lines.

However, I'm assuming there could be a (low memory), up to date API for doing simple large File operations with a minimal amount of boiler plate for java --- Does any such library or functionality exist anywhere in the any of the Google, Apache, etc... open-source Java utility libraries ?


回答1:


Java 8 short way:

 Files.lines(Paths.get(fileName)).count();

But most memory effiecint:

try(InputStream in = new BufferedInputStream(new FileInputStream(name))){
    byte[] buf = new byte[4096 * 16];
    int c;
    int lineCount = 0;
    while ((c = in.read(buf)) > 0) {
       for (int i = 0; i < c; i++) {
           if (buf[i] == '\n') lineCount++;
       }
    }
}

You do not need String objects in this task at all.




回答2:


With Guava:

int nLines = Files.readLines(file, charset, new LineProcessor<Integer>() {
  int count = 0;
  Integer getResult() {
    return count;
  }
  boolean processLine(String line) {
    count++;
    return true;
  }
});

which won't hold the whole file in memory or anything.




回答3:


Without a library:

public static int countLines(String filename) throws IOException {
    int count = 0;
    BufferedReader br = new BufferedReader(new FileReader(filename));
    try {
        while (br.readLine() != null) count++;
    } finally { 
        br.close(); 
    }
    return count;
}



回答4:


Here's a version that makes use of Apache Commons IO library. You can pass null for encoding to pick the platform default.

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;

public static long countLines(String filePath, String encoding)
throws IOException {
    File file = new File(filePath);
    LineIterator lineIterator = FileUtils.lineIterator(file, encoding);
    long lines = 0;
    try {
        while ( lineIterator.hasNext() ) {
            lines++;
            lineIterator.nextLine();
        }
    } finally {
        LineIterator.closeQuietly( lineIterator );
    }
    return lines;
}


来源:https://stackoverflow.com/questions/9691420/api-for-simple-file-line-count-functions-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!