Java IO implementation of unix/linux "tail -f"有類似的問題;但該解決方案對於每秒產生大約50-100行的日誌文件不可行。Java - RandomAccessFile(模擬Linux尾部函數)
我有一個算法模擬Linux中的尾部功能。例如,
File _logFile = new File("/tmp/myFile.txt");
long _filePtr = _logFile.length();
while (true)
{
long length = _logFile.length();
if (length < _filePtr)
{
// means file was truncated
}
else if (length > _filePtr)
{
// means something was added to the file
}
// we ignore len = _filePtr ... nothing was written to file
}
我的問題是,當:「東西被添加到文件」(指其他if()語句)。
else if (length > _filePtr)
{
RandomAccessFile _raf = new RandomAccessFile(_logFile, "r");
raf.seek(_filePtr);
while ((curLine = raf.readLine()) != null)
myTextPane.append(curLine);
_filePtr = raf.getFilePointer();
raf.close();
}
在當程序塊((curLine = raf.readLine())......的運行時間在15秒後(注:。該程序正確運行的第15秒)
看來,raf.readLine()是從來沒有打空,因爲我相信這個日誌文件被寫入如此之快,我們進入了「無盡的貓捉老鼠」的循環。
什麼是模仿的最好方式Linux的尾巴?
感謝凱文!你能澄清你的最後一段「EDT」的含義嗎? – 2010-10-28 14:01:39
事件調度線程 - 這個短語的快速谷歌會給你你需要知道的。長與短:對Swing組件進行狀態更改是無效的,除非您使用EDT。 – 2010-10-28 16:38:51