Java:检测文件修改(文件轮询?)

发布于 2024-09-26 16:45:05 字数 384 浏览 0 评论 0原文

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

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

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

我知道 Java 7 的 WatchService,但由于 7 尚未正式发布,我对使用该方法或 JNI 库犹豫不决。

非常感谢!

编辑:看来文件轮询是可行的方法。如果有人可以推荐最安全/有效的方法来实现文件轮询,那就太好了。

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

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?

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.

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.

Much appreciated!

EDIT: 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

番薯 2024-10-03 16:45:05

以下是两个可用的库:

Java 7 NIO2 可能也有一些适合你的东西(我不知道这些功能目前是否在 Java 7 的范围内)也可以作为 Java 7 NIO2 获得。 sun.com/developer/technicalArticles/javase/nio/" rel="nofollow">JSR 203 请参阅

Here are two avaliable libraries:

Java 7 NIO2 might also have some things for you ( I dont know if thease features are in scope for Java 7 at the moment) Also avaliable as JSR 203 see here

千纸鹤带着心事 2024-10-03 16:45:05

没有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文