用于创建/删除/等的Java文件夹监视器?
有没有一种方法可以监视java中的文件夹以查看它们何时更新,即文件创建、删除、重命名等。 还有什么方法可以设置它只查找以某个前缀(即 img)开头的文件?
Is there a way to monitor folders in java to see when they are updated, ie file created, deleted, renamed,etc. And also is there any way to set it that it only looks for files that begin with a certain prefix, ie img?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Java 7 中,有一个
WatchService
API 作为 NIO2 的一部分。 对于早期的 Java 版本,除了手动轮询之外,没有纯 Java 解决方案。In Java 7 there is a
WatchService
API as part of NIO2. For earlier Java versions there's no pure Java solution except for manual polling.您可以使用 inotify-java API,它提供了基于事件的机制,使用 glibc(2.4 版及更高版本)和 Linux 内核(从 2.6.13 开始)提供的 inotify 接口来监视 Linux 文件系统事件。
特征
简单易用,功能齐全,支持 inotify
低内存占用 API 使用阻塞调用,从而降低 CPU 使用率
You can use inotify-java API that provides an event-based mechanism for monitoring Linux file system events using the inotify interface provided by glibc (versions 2.4 and up) and the Linux kernel, starting from 2.6.13.
Features
Simple, easy to use feature-complete support for inotify
Low memory footprint API uses blocking calls yielding low CPU usage
请参阅我的回答< /a> 类似的问题。
详细的机制不会过滤文件名/类型。 您必须与建议的解决方案一起执行此操作。
See my answer to a similar question.
The mechanisms detailed won't filter on a file name/type. You'll have to do that alongside the suggested solutions.