在写入文件时读取文件

发布于 2024-10-07 13:51:58 字数 1089 浏览 8 评论 0原文

我必须读取tomcat日志文件,一段时间后(例如:一小时)我将再次读取该文件(仅用于新添加的内容),因此我创建RandomAccessFile来记录我完成的最后一个帖子,并使用BufferedReader.readLine() 方法。

但是,我发现有时我无法读取文件的整行。

例如,tomcat尝试写入以下内容(仅示例):

192.168.0.0本地主机/index.html.....

此时,当我阅读时,我可能会得到结果:

192.168.0 0 localhost /index.html .....

192.168.0.0本地主机/index.html.....

也就是说,如果正在写入此行,我的读者将读取到不完整的行。

所以我想知道有什么想法可以决定正在读取的行是否已完成?

这是核心代码:

raf = new RandomAccessFile(file, "r");
raf.seek(pos);
while ((line = raf.readLine()) != null) {
    System.out.println(line);
}
pos = raf.getFilePointer();
raf.close();

我已经尝试过这个(添加条件):

raf = new RandomAccessFile(file, "r");
raf.seek(pos);
while ((line = raf.readLine()) != null) {
    if(line.chartAt(line.length()-1)=='\n'){
        System.out.println(line);
    } else
        raf.seek(pos); //roll back to the last position

}
pos = raf.getFilePointer();
raf.close();

但它不起作用..

有什么想法吗?

I have to read the tomcat log file,and after some time(for example:one hour) I will read the file again(just for the new added content),so I create the RandomAccessFile to record the last postion I completed,and use the BufferedReader.readLine() method.

However,I found that sometime I can not read the whole line of the file.

For example,the tomcat is tring to write the following content(just example):

192.168.0.0 localhost /index.html .....

And at this moment,when I read I may get the result :

192.168.0 0 localhost /index.html .....

or

192.168.0.0 localhost /index.html .....

That's to say,my reader read a uncomplete line if this line is being written.

So I wonder any idea to decide if the line which is being reading has been completed?

This is the core code:

raf = new RandomAccessFile(file, "r");
raf.seek(pos);
while ((line = raf.readLine()) != null) {
    System.out.println(line);
}
pos = raf.getFilePointer();
raf.close();

I have tried this(add the contidtion):

raf = new RandomAccessFile(file, "r");
raf.seek(pos);
while ((line = raf.readLine()) != null) {
    if(line.chartAt(line.length()-1)=='\n'){
        System.out.println(line);
    } else
        raf.seek(pos); //roll back to the last position

}
pos = raf.getFilePointer();
raf.close();

But it does not work..

ANy idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夕嗳→ 2024-10-14 13:51:58

readLine 消耗换行符。您不能用它来确定是否有完整的线路。您需要一次读取一个字节并自己解析该行。

当您获得有效线路时,您也不会在该位置上移动。

readLine consumes the newline character. You can't use it to determine if you have a complete line or not. You need to read one byte at a time and parse the line yourself.

Also you are not moving on the position when you get a valid line..

我偏爱纯白色 2024-10-14 13:51:58

readLine() 会去除 EOL 字符,这样您就看不到它了。从您的情况来看,EOF 和 EOL 之间没有区别,这令人不安。除了 Peter 的解决方案之外,您还可以尝试在读取该行后向后查找一个字节并检查它是否已 EOL。不过,要小心使用的行结束样式。

readLine() strips EOL characters so you don't see it. There is no difference between EOF and EOL from its point of view which is troubling in your case. In addition to Peter's solution, you can try seeking one byte back after you read the line and check if it was EOL or not. Be careful about line ending style being used, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文