我有一个进程(实际上是一个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