public static void main(String args[]) {
RandomAccessFile rf = null;
try {
rf = new RandomAccessFile("d:\\a.txt", "r");
long len = rf.length();
long start = rf.getFilePointer();
long nextEnd = start + len - 1;
String line = null;
rf.seek(nextEnd);
int c = -1;
while (nextEnd > start) {
c = rf.read();
if (c == '\n' || c == '\r') {
line = rf.readLine();
if (StringUtils.isNotEmpty(line)) {
System.out.println(line);
//最后一行
return;
}
}
nextEnd--;
rf.seek(nextEnd);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (rf != null)
rf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java 读取文件最后一行
最新推荐文章于 2022-04-02 17:15:21 发布