Java阅读第n行 [英] Java reading nth line

查看:303
本文介绍了Java阅读第n行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个文本文件中读取特定的行,但是我不想将这个文件加载到内存中(它可以变得很大)。

我一直在寻找,但我发现的每一个例子都需要阅读每一行(这会减慢我的代码,因为有超过100000行)或加载到一个数组,并获得正确的元素(文件将有很多

一个我想要做的例子:

  String line = File.getLine(5); 

代码不是实际的代码,它是用来显示我想要的原理 / p>

有没有办法做到这一点?

-----编辑-----

>

我刚刚意识到这个文件也会被写入到读取行之间(添加到文件尾部)。





$ p
$ b $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'除非行数是固定的字节数,否则不行。



你不必在存储器中保存每一行,但你必须仔细阅读整个文件才能找到你想要的行,否则你将不知道从哪里开始阅读。


I am trying to read a specific line from a text file, however I don't want to load the file into memory (it can get really big).

I have been looking but every example i have found requires either to read every line (this would slow my code down as there are over 100,000 lines) or load the whole thing into an array and get the correct element (file will have alot of lines to input).

An example of what I want to do:

String line = File.getLine(5);

"code is not actual code, it is made up to show the principle of what i want"

Is there a way to do this?

-----Edit-----

I have just realized this file will be written too in between reading lines (adding to the end of the file).

解决方案

Is there a way to do this?

Not unless the lines are of a fixed number of bytes each, no.

You don't have to actually keep each line in memory - but you've got to read through the whole file to get to the line you want, as otherwise you won't know where to start reading.

这篇关于Java阅读第n行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆