使用Java怎么读取文本指定的某一行内容

发布时间:2021-05-18 17:42:40 作者:Leah
来源:亿速云 阅读:710

本篇文章为大家展示了使用Java怎么读取文本指定的某一行内容,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

public class OpenTextLine {
	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 = 9999999;//设置指定行数
		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是什么

Java是一门面向对象编程语言,可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序。

上述内容就是使用Java怎么读取文本指定的某一行内容,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. python怎么读取csv文件指定行
  2. python如何读取指定字节长度的文本

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:怎么在Java中使用DOM4J方式生成XML

下一篇:怎么在java中使用hasNextInt判断是否为数字

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》