首页 > 编程语言 > 详细

Java读取文本指定的某一行内容的方法

时间:2019-03-04 10:07:22      阅读:342      评论:0      收藏:0      [点我收藏+]
    public static void main(String[] args) throws IOException {
        long timeStart = System.currentTimeMillis();
        File file = new File("testData.txt");//文件路径
        FileReader fileReader = new FileReader(file);
        LineNumberReader reader = new LineNumberReader(fileReader);
        int number = 99;//设置指定行数
        String txt = "";
        int lines = 0;
        while (txt != null) {
            lines++;
            txt = reader.readLine();
            if (lines == number) {
                System.out.println("第" + reader.getLineNumber() + "的内容是:" + txt + "\n");
                long timeEnd = System.currentTimeMillis();
                System.out.println("总共花费:" + (timeEnd - timeStart) + "ms");
                System.exit(0);
            }
        }
        reader.close();
        fileReader.close();
    }
}

Java读取文本指定的某一行内容的方法

原文:https://www.cnblogs.com/ysySelf/p/10468909.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!