Java文件尾部实现 [英] Java file tailer implementation

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

问题描述

实现一个与tail -f somefile

我已经阅读了一些类似的问题.而且我看到了一些选择.

I've read a few similar questions. and I've seen a few options.

  1. 使用BufferedReader,基本思想是使用缓冲的读取器从文件中读取.如果返回null,则睡眠几秒钟,然后继续无限循环.我对此进行了一些实验,只要您读到文件末尾,我的结果就会显示出来,然后getLine方法将不再为您提供任何更新.那这种方法真的有用吗?

  1. using BufferedReader, basic idea is to use buffered reader to read from the file. if null is returned then sleep a few seconds and then continue in a infinite loop. I experimented a bit on this, my result show as long as you read to the end of the file, then getLine method will no longer give you any updates. so does this approach work at all?

使用random access file.每次进行读取操作时,请创建一个随机访问文件,并将文件长度与历史文件进行比较,如果当前文件较长,则尝试最后一次读取和读取增量部分.我敢肯定这是可行的,但是每次读取都会打开一个新的随机访问文件,是不是有一种更有效的方法?

using random access file. every time making a read operation, create a random access file and compare the file length with historical one, if current one is longer, then seek to last read and read in the delta parts. I'm sure this works, but with a new random access file opened each time read, isn't there a more efficient approach ?

我已经看到新的JDK在缓冲文件阅读器中添加了stream API,我想这与附加在尾部的新内容无关.它只与最初给出的内容有关.我的问题是,可以将此流api扩展为考虑tailer function吗?

I've seen the new JDK added stream API into buffered file reader, I guess this has nothing to do with the new content appended at the tail. it's only related what was first given. my question is could this stream api be extended to take the tailer function into consideration ?

问题:

  • 可以使用BufferedReader来实现tail -f吗?在我的情况下,一旦我读取了通行证EOF,就只会返回null.

  • can BufferedReader be used to implement tail -f ? in my case, once I read pass EOF, only null is returned.

可以使用JDK8 stream来实现tail -f吗?

can JDK8 stream be used to implement tail -f?

除了像apache common lib这样的重复打开关闭文件以外,还有其他更有效的实现吗?

is there more efficient implementation other than repeatedly open close file like apache common lib?

推荐答案

我最终使用了一些Apache库来解决此问题. (一旦我记住它就会更新).

I eventually used some Apache library to fix this. (will update once I remember it).

基本上,文件监视程序取决于文件系统API.在linux发行版上,通过EOF读取可能会很好,并带来以后附加的新内容.

essentially the file watchers are file system API dependent. on linux distributions, read over EOF would probably be fine and bring new contents that appended later.

我观察到的问题是在MACO上,一旦您在EOF之后读取,该文件处理程序将不再有效.如果您知道要读取新内容,则必须重新打开文件.

the issue I observed is on MACOs, where once you read after EOF that file handler is no longer valid. you have to reopen the file if you know you are reading new content.

  • 这是在MacOs Maveriks上使用的,我没有测试过最新版本是否仍然相同.
  • 这也是基于JDK api的,而不是基于底层MACOS文件系统API的.因为我觉得less /F tail -f应该更有效地实现.
  • this was on MacOs Maveriks and I didn't test it's still the same on latest versions.
  • also this was based on the JDK api, not underlying MACOS file system API. because I felt less /F tail -f should have been implemented more efficiently.

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

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