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

查看:118
本文介绍了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.

可以接受大约一秒或2秒的延迟,但不再可以.

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天全站免登陆