搬砖 - java快速获取文件行数

    public static int getLineCount(File file) {
        long startTime = System.currentTimeMillis();
        int lines = 0;
        try (
                FileReader in = new FileReader(file);
                LineNumberReader reader = new LineNumberReader(in);
            ){
            reader.skip(Long.MAX_VALUE);
            lines = reader.getLineNumber();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();

        System.out.println("------------- time spent: " + (endTime - startTime) + "ms -------------");
        return lines;
    }

转载自:https://www.cnblogs.com/ganhaiqiang-20130831/articles/4937290.html

posted @ 2020-10-16 19:04  雾中的-松  阅读(490)  评论(0编辑  收藏  举报