Java:读取一个巨大文件的最后 n 行 [英] Java : Read last n lines of a HUGE file

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

问题描述

我想读取一个非常大文件的最后 n 行,而不是使用 Java 将整个文件读入任何缓冲区/内存区域.

I want to read the last n lines of a very big file without reading the whole file into any buffer/memory area using Java.

我环顾了 JDK API 和 Apache Commons I/O,但找不到适合此目的的一个.

I looked around the JDK APIs and Apache Commons I/O and am not able to locate one which is suitable for this purpose.

我在想在 UNIX 中使用 tail 或 less 的方式.我认为他们不会加载整个文件然后显示文件的最后几行.在 Java 中也应该有类似的方法来做同样的事情.

I was thinking of the way tail or less does it in UNIX. I don't think they load the entire file and then show the last few lines of the file. There should be similar way to do the same in Java too.

推荐答案

如果您使用 RandomAccessFile,你可以使用lengthseek 到达文件末尾附近的特定点,然后读取从那里向前.

If you use a RandomAccessFile, you can use length and seek to get to a specific point near the end of the file and then read forward from there.

如果您发现行数不足,请从该点备份并重试.一旦您确定了 N 的最后一行开始的位置,您就可以寻找到那里并且只是读取和打印.

If you find there weren't enough lines, back up from that point and try again. Once you've figured out where the Nth last line begins, you can seek to there and just read-and-print.

可以根据您的数据属性做出初始的最佳猜测假设.例如,如果它是一个文本文件,它的平均行长度可能不会超过 132,因此,要获取最后 5 行,请在结束前 660 个字符开始.然后,如果你错了,在 1320 再试一次(你甚至可以用你从最后 660 个字符中学到的东西来调整它——例如:如果那 660 个字符只有三行,那么下一次尝试可能是 660/3 * 5,加上可能有点额外以防万一).

An initial best-guess assumption can be made based on your data properties. For example, if it's a text file, it's possible the line lengths won't exceed an average of 132 so, to get the last five lines, start 660 characters before the end. Then, if you were wrong, try again at 1320 (you can even use what you learned from the last 660 characters to adjust that - example: if those 660 characters were just three lines, the next try could be 660 / 3 * 5, plus maybe a bit extra just in case).

这篇关于Java:读取一个巨大文件的最后 n 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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