您可以使用RandomAccessFile。您可以将指针存储到您有红色的最后一个字节,如下所示:
String pathToYourFile = "/path/to/your/logfile";
long lastBytePosition = 0;
boolean shouldStop = false;
while (! shouldStop) {
Thread.sleep(2000);
File f = new File(pathToYourFile);
long length = f.length();
RandomAccessFile raf = new RandomAccessFile(f, "r");
byte[] buff = new byte[(int) (length - lastBytePosition)];
raf.readFully(buff, (int) lastBytePosition, (int) (length - lastBytePosition));
shouldStop = processChunk(buff);
lastBytePosition = (int) length;
}
... whereprocessChunk
是一种处理来自文件的新输入的方法。
这远非卓越,但我想你明白了。