简体   繁体   English

我如何跳到特定行并在Java中从中读取

[英]How can I jump to specific line and read from that in java

I meet a big file(much GB),and I want to jump to specific line directly, and then read some line from that... 我遇到一个大文件(很大GB),我想直接跳到特定行,然后从中读取一些行...

for example, I hava file like 例如,我喜欢

1.aaaaaaaaaaaa
2.bbbbbbbbbbbb
3.cccccccccccc
4.dddddddddddd

and want to read lines from 3 and 4. now doesn't using 'readLine()' for handling 1....2 line, but staring my journey at 3 and read 2 lines. 并且想要读取3和4中的行。现在不使用'readLine()'处理1 .... 2行,而是将我的旅程盯着3并读取2行。

how can I do that in java? 如何在Java中做到这一点? ....because I doesn't want to let much objects in memory... ....因为我不想在内存中放太多对象...

thank u! 感谢你!

A new line in a file is just a character. 文件中的新行只是一个字符。 It is the same in Java, C and any other language, you'll have to use readLine() or similar method to count the lines. 在Java,C和任何其他语言中都是相同的,您必须使用readLine()或类似的方法来计算行数。 Even if there is a library that'll do it for you, it will still have to go char by char to count the lines. 即使有一个库可以帮您完成,它仍然必须逐个字符地计算行数。

You don't have to store the value returned by readLine() at each calls, just check if it starts by the value you want. 您不必在每次调用时都存储readLine()返回的值,只需检查它是否以所需的值开头即可。 If it does, then you can store the lines you want. 如果是这样,则可以存储所需的行。

If you know the offset you want to jump at (and not only a line number), then you could use a RandomAccessFile and the skip method. 如果知道要跳转的偏移量(不仅是行号),则可以使用RandomAccessFile和skip方法。 In your case, if your lines are really all equals, you could compute the offset and jump. 在您的情况下,如果您的线实际上完全相等,则可以计算偏移量和跳跃数。

Otherwise, if you just base your jump on line numbers, you will have to read all the file, line by line using a BufferedReader or using a FilterReader or by buffering a huge tab of chars and counting line by yourself, whatever you want, and start considering only the portion of data you want. 否则,如果您仅基于行号进行跳转,则必须使用BufferedReader或FilterReader逐行读取所有文件,或者通过缓冲巨大的char标签并自己计算行数来随意读取所有文件,并且开始只考虑所需的数据部分。

Another good option for a huge volume of data is a database... 对于海量数据的另一个不错的选择是数据库...

Regards, Stéphane 问候,斯特凡

Do not use readline() as it will allocate useless String . 不要使用readline() ,因为它将分配无用的String Call read() on a BufferedReader , counting the number of '\\n' until you have skip the number of lines you want. BufferedReader上调用read() ,计算'\\n'的数量,直到跳过所需的行数。

Edit : 编辑

You might also have to count the `\\r' and '\\r' immediately followed by a '\\n' to do exactly the same as readline() . 您可能还必须立即计数`\\r''\\r' ,后跟一个'\\n'来做与readline()完全相同的操作。 You might have a small problem when you read the last '\\r' as you cannot know if it is followed by a '\\n' or not. 当您读最后一个'\\r'时,可能会遇到一个小问题,因为您不知道它后面是否跟有'\\n' To handle this case, I woult read the next char, and if it is not a '\\n' , I would use it in front of the 1st important line. 为了处理这种情况,我将读取下一个字符,如果它不是'\\n' ,我将在第一个重要行的前面使用它。

Another solution if your lines are of fixed size as in your example it to compute the number of chars to skip and use the BufferedReader.skip() method. 如果您的行的大小是固定的(如您的示例中所示),则另一种解决方案是计算要跳过的字符数,并使用BufferedReader.skip()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM