我有一個進程(實際上是一個web服務器),它將從客戶端進入的POST作爲日誌文件寫入日誌文件。BufferedReader readline在行完成之前
然後,我有一個單獨的進程,倒像是在一個循環(尾-f)日誌文件:
BufferedReader reader = new BufferedReader(new FileReader(logFilePath));
String line = null;
while (true)
{
if ((line = reader.readLine()) != null)
{
if (firstLine == null) firstLine = line;
processLine(line);
continue;
}
try
{
Thread.sleep(sleepTime);
long ts = System.currentTimeMillis();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
這工作的時間非常好99%,但偶爾,行我通過閱讀BufferedReader.readLine不是一個完整的行。我想這是因爲作家(網絡服務器)尚未完成將該行的所有字節清除到文件。
任何修復或解決此問題的建議?非常感謝!
你有一種模式來檢測線路不完整? – VirtualTroll 2012-02-24 19:16:36
只需一個快速指針:你不需要使用'while(true)... break',你可以使用一個變量來檢查是否應該繼續。 – Viruzzo 2012-02-24 19:18:02
我認爲這可能會對您有所幫助: http://docs.oracle.com/javase/1.4.2/docs/api/java/io/FileDescriptor.html#sync() – nullpotent 2012-02-24 19:19:16