Java:读取HUGE文件的最后n行 [英] Java : Read last n lines of a HUGE file
问题描述
我环顾了JDK API和Apache Commons I / O,无法找到适合此目的的应用程序。
我正在考虑在UNIX中使用tail或less的方式。我不认为他们加载整个文件,然后显示文件的最后几行。如果你使用一个 /download.oracle.com/javase/6/docs/api/java/io/RandomAccessFile.htmlrel =noreferrer> RandomAccessFile
,您可以使用 长度
和 seek
到文件末尾的特定位置,然后从那里读取。
如果发现没有足够的线条,请从这一点备份并重试。一旦你发现了 N
最后一行开始的地方,你可以到那里去阅读和打印。
可以根据您的数据属性进行初始的最佳猜测假设。例如,如果是文本文件,则行长度可能不会超过平均值132,因此,要获取最后五行,请在结束之前启动660个字符。那么,如果你错了,就试试1320(甚至可以使用你从最后660个字符中学到的东西来调整 - 例如:如果这660个字符只是三行,下一个尝试可能是660/3 * 5,加上也许多一点,以防万一)。
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.
I looked around the JDK APIs and Apache Commons I/O and am not able to locate one which is suitable for this purpose.
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.
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.
If you find there weren't enough lines, back up from that point and try again. Once you've figured out where the N
th last line begins, you can seek to there and just read-and-print.
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:读取HUGE文件的最后n行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!