API for simple File (line count) functions in Java

后端 未结 4 1140
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-06 05:12

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

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

Howeve

4条回答
  •  北恋
    北恋 (楼主)
    2021-01-06 05:45

    With Guava:

    int nLines = Files.readLines(file, charset, new LineProcessor() {
      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.

提交回复
热议问题