java读文件最后一行_Java快速阅读文本文件的最后一行?

小编典典

以下是两个函数,一个函数返回文件的最后一个非空白行而不加载或单步浏览整个文件,另一个函数返回文件的最后N行而不单步浏览整个文件:

尾部的作用是直接缩放到文件的最后一个字符,然后逐个字符向后退一步,记录所看到的内容,直到找到换行符为止。找到换行符后,便会跳出循环。反转记录的内容,并将其放入字符串中并返回。0xA是新行,0xD是回车。

如果你的行尾是\r\n或crlf或其他“双换行符样式换行符”,那么你将必须指定n * 2行才能获得最后n行,因为每行计数2行。

public String tail( File file ) {

RandomAccessFile fileHandler = null;

try {

fileHandler = new RandomAccessFile( file, "r" );

long fileLength = fileHandler.length() - 1;

StringBuilder sb = new StringBuilder();

for(long filePointer = fileLength; filePointer != -1; filePointer--){

fileHandler.seek( filePointer );

int readByte = fileHandler.readByte();

if( readByte == 0xA ) {

if( filePointer == fileLength ) {

continue;

}

break;

} else if( readByte == 0xD ) {

if( filePointer == fileLength - 1 ) {

continue;

}

break;

}

sb.append( ( char ) readByte );

}

String lastLine = sb.reverse().toString();

return lastLine;

} catch( java.io.FileNotFoundException e ) {

e.printStackTrace();

return null;

} catch( java.io.IOException e ) {

e.printStackTrace();

return null;

} finally {

if (fileHandler != null )

try {

fileHandler.close();

} catch (IOException e) {

/* ignore */

}

}

}

但是你可能不想要最后一行,而想要最后N行,因此请改用以下代码:

public String tail2( File file, int lines) {

java.io.RandomAccessFile fileHandler = null;

try {

fileHandler =

new java.io.RandomAccessFile( file, "r" );

long fileLength = fileHandler.length() - 1;

StringBuilder sb = new StringBuilder();

int line = 0;

for(long filePointer = fileLength; filePointer != -1; filePointer--){

fileHandler.seek( filePointer );

int readByte = fileHandler.readByte();

if( readByte == 0xA ) {

if (filePointer < fileLength) {

line = line + 1;

}

} else if( readByte == 0xD ) {

if (filePointer < fileLength-1) {

line = line + 1;

}

}

if (line >= lines) {

break;

}

sb.append( ( char ) readByte );

}

String lastLine = sb.reverse().toString();

return lastLine;

} catch( java.io.FileNotFoundException e ) {

e.printStackTrace();

return null;

} catch( java.io.IOException e ) {

e.printStackTrace();

return null;

}

finally {

if (fileHandler != null )

try {

fileHandler.close();

} catch (IOException e) {

}

}

}

像这样调用以上方法:

File file = new File("D:\\stuff\\huge.log");

System.out.println(tail(file));

System.out.println(tail2(file, 10));

2020-03-10

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值