我想读取一个非常大的文件的最后n行,而不是使用Java将整个文件读入任何缓冲区/内存区域.
我查看了JDK API和Apache Commons I/O,但无法找到适合此目的的API.
我在想UNIX中的尾部或更少的方式.我不认为他们加载整个文件,然后显示该文件的最后几行.在Java中也应该有类似的方法来做同样的事情.
我想从结束到开始我的文件的相反方向读取文件,
[1322110800] LOG ROTATION: DAILY
[1322110800] LOG VERSION: 2.0
[1322110800] CURRENT HOST STATE:arsalan.hussain;DOWN;HARD;1;CRITICAL - Host Unreachable (192.168.1.107)
[1322110800] CURRENT HOST STATE: localhost;UP;HARD;1;PING OK - Packet loss = 0%, RTA = 0.06 ms
[1322110800] CURRENT HOST STATE: musewerx-72c7b0;UP;HARD;1;PING OK - Packet loss = 0%, RTA = 0.27 ms
Run Code Online (Sandbox Code Playgroud)
我用代码以这种方式阅读它,
String strpath="/var/nagios.log";
FileReader fr = new FileReader(strpath);
BufferedReader br = new BufferedReader(fr);
String ch;
int time=0;
String Conversion="";
do {
ch = br.readLine();
out.print(ch+"<br/>");
} while (ch != null);
fr.close();
Run Code Online (Sandbox Code Playgroud)
我更喜欢使用缓冲读取器以相反的顺序读取
我有一个非常大的文件(甚至可能是1G),我想以相反的顺序(在Java中)创建一个新文件.例如:
Original file:
This is the first line
This is the 2nd line
This is the 3rd line
The reversed file:
This is the 3rd line
This is the 2nd line
This is the first line
Run Code Online (Sandbox Code Playgroud)
由于文件非常大,一次将整个文件加载到内存并反转顺序可能会有问题(我可以使用的内存有限).我怎样才能在Java中实现这一目标?
谢谢