JAVA快速读取文本最后一行

// 使用RandomAccessFile , 从后找最后一行数据
RandomAccessFile raf = new RandomAccessFile("E:/demo/data.dat", "r");
long len = raf.length();
String lastLine = "";
if (len != 0L) {
  long pos = len - 1;
  while (pos > 0) { 
    pos--;
    raf.seek(pos);
    if (raf.readByte() == '\n') {
      lastLine = raf.readLine();
      break;
    }
  }
}
raf.close();
System.out.println(lastLine);

你可能感兴趣的:(java)