Java 实时读取日志文件 [英] Java read a logfile live

查看:35
本文介绍了Java 实时读取日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Java 编写 cod4 服务器控制器(我知道那里有非常好的服务器控制器,但我想从中学习).现在我想根据日志文件中的条目执行特定操作,这个文件经常被 cod 更新,并且文件可能会变得非常大.现在我如何有效地只读取文件中已更改的部分,每隔一秒左右?

I'm writing a cod4 server controller in Java(I know there are perfectly fine server controllers out there, but I want to learn from it). Now I want to take specific actions according to entries in a logfile, this file is updated by cod very often, and the file can get quite large. Now how do I efficiently read only the part that has changed of the file, every second or so?

或者有没有办法将日志文件中更改的所有内容实时发送到 Java?(我读了一些关于管道的内容).服务器运行在 linux 上.不需要将日志文件仍然保存在同一位置,因为一切都应该通过 Java 我可以用它保存.

Or is there a way to send everything that is changed in the logfile live to Java?(I read something about pipes). The server runs on linux. It's not needed that the logfile is still saved in the same location, since everything should go through Java I can just save it with that.

大约一秒或两秒的延迟是可以接受的,但不能再延迟了.

A delay of about a second or 2 is acceptable, but not any longer.

推荐答案

也许你可以执行一个 'tail -f logfile.txt' 子进程并监控输出流?

Maybe you could execute a 'tail -f logfile.txt' subprocess and monitor the output stream?

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html

这篇关于Java 实时读取日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆