API for simple File (line count) functions in Java

后端 未结 4 1111
爱一瞬间的悲伤
爱一瞬间的悲伤 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<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.

    0 讨论(0)
  • 2021-01-06 05:58

    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;
    }
    
    0 讨论(0)
  • 2021-01-06 06:04

    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.

    0 讨论(0)
  • 2021-01-06 06:05

    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;
    }
    
    0 讨论(0)
提交回复
热议问题