Most efficient way to tail/poll a log file in Java [duplicate]

你离开我真会死。 提交于 2019-12-03 03:46:56

If you can use Java 7 there are File Watchers and you could implement what you need.
File Notifications

For Java 6 (and previous) you would have to use Apache Commons VFS (File Monitor) lib to create your own file watcher.

If you can't go to Java 7 or use third party libs (e.g commons or jnotify) you would have to write a file watchdog your self using polling threads.

You can use java nio's WatchService to get asynchronous notifications if a file/directory has changed. This tutorial explains the way to use it.

What you can do to simplify this is to create a Thread which poll the size of the file and only reads that much i.e. it never reaches the end as such. This data read can be written to a pipe allowing your main thread to read a continous pipe.

I don't know answer. Just two tips
1. Maybe RandomAccessFile would help you.
2. Try find another open source program, which do this. (e.g. IntelliJ IDEA do exactly this)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!