java快速获取文件行数
1 public int getTotalLines(File file) throws IOException { 2 long startTime = System.currentTimeMillis(); 3 FileReader in = new FileReader(file); 4 LineNumberReader reader = new LineNumberReader(in); 5 reader.skip(Long.MAX_VALUE); 6 int lines = reader.getLineNumber(); 7 reader.close(); 8 long endTime = System.currentTimeMillis(); 9 10 System.out.println("统计文件行数运行时间: " + (endTime - startTime) + "ms"); 11 return lines; 12 }
经测试获取100万行的txt文件总行数耗时180~210毫秒之间