Number of lines in a file in Java

前端 未结 19 2352
抹茶落季
抹茶落季 2020-11-22 05:31

I use huge data files, sometimes I only need to know the number of lines in these files, usually I open them up and read them line by line until I reach the end of the file<

19条回答
  •  一生所求
    2020-11-22 06:09

    How about using the Process class from within Java code? And then reading the output of the command.

    Process p = Runtime.getRuntime().exec("wc -l " + yourfilename);
    p.waitFor();
    
    BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = "";
    int lineCount = 0;
    while ((line = b.readLine()) != null) {
        System.out.println(line);
        lineCount = Integer.parseInt(line);
    }
    

    Need to try it though. Will post the results.

提交回复
热议问题