Java:检测对文件的修改(文件轮询?) [英] Java: Detecting modification to a file (file polling?)

查看:414
本文介绍了Java:检测对文件的修改(文件轮询?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检测Java中文件修改的最有效方法是什么?

What is the most efficient way to detect modification to a file in Java?

我读过关于文件轮询但是,轮询方法有许多缺点随着观看文件数量的增加而变得明显。我宁愿不使用轮询,但在线的一些解决方案似乎指向文件轮询作为推荐的方法。我希望可能是一个与操作系统相关的文件系统更新回调解决方案 - 这在Linux和Windows中都可能吗?

I've read about file polling however, the polling approach has a number of drawbacks that become apparent as the number of watched files increases. I rather not use polling but a number of solutions online appear to point to file polling as the recommended method. I was hoping for perhaps an OS-related file system update callback solution instead -- is this possible in both linux and windows?

鉴于我目前只打算观看2个文件,我仍然对文件轮询解决方案持开放态度。理想情况下,我希望文件轮询不会对性能产生很大影响。

Given that I currently only intend to 'watch' 2 files, I'm still kind of open to a file polling solution. Ideally, I'm hoping file polling won't have a great impact on performance.

我知道Java 7的WatchService,但是因为7还没有正式发布,我对使用这种方法或JNI库犹豫不决。

I'm aware of Java 7's WatchService but since 7 hasn't been officially released, I'm hesitant to use that approach or JNI libraries.

非常感谢!

编辑:文件轮询似乎是要走的路。如果有人可以推荐最安全/最有效的方式来实现文件轮询,那就太棒了。

It appear file polling is the way to go. If anyone can recommend the most safest/efficient way to implement file polling, that would be great.

推荐答案

没有Java解决方案让文件操作系统在文件更改时通知您。它可能通过本机调用,但每个操作系统在这方面会有所不同,而且Java当然不支持它。它需要大量的JNI自定义编码 - 如果它甚至可能。

There is no Java solution to have the OS notify you when a file changes. It may be possible through native calls, but each OS would be different in that regard, and it certainly isn't supported in Java. It would require a lot of custom coding with JNI - if it's even possible.

使用轮询解决方案,只要您轮询修改日期,它应该是漂亮的快。请确保不要长时间保持对文件的引用。始终在轮询周期之间关闭它们以确保不会产生冲突。

Going with the polling solution, as long as you poll for date modified, it should be pretty quick. Just be sure to not leave references to files open for extended periods of time. Always close them between polling cycles to ensure that you don't create conflicts.

这篇关于Java:检测对文件的修改(文件轮询?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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