栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

如果你的行尾是

rn
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) {      }    }}

但是你可能不想要最后一行,而想要最后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));


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/411090.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号