java - 是否可以使用 java 代码监视文件夹?

标签 java monitoring


有谁知道如何使用 java 监视文件夹?或者任何人都可以给我一个观点,我该如何开始。这是我的想法。

start a thread to scan the folder changes,which could be create,delete,update files in this folder or something else happen,like last updated.
but in this case, you have to control thread loop. if this thread loop is not controlled well,then it would be a waste of cpu and may cause a fatal problem.

或者,是否有任何框架或一些演示代码可以执行此操作?希望我们能找到更好的方法来做到这一点。非常感谢你。

最佳答案

JDK7 的 java.nio.file 包有一个 WatchService支持文件更改通知。您可以使用它来监视目录的更改。这在可用的情况下使用 native 设施,否则它使用原始机制,例如轮询。 Here是关于它的帖子。

现在,您可以尝试 jpathwatch ,它是 WatchService 接口(interface)的实现,使用 native OS 功能,而不是轮询。

关于java - 是否可以使用 java 代码监视文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3810790/

相关文章:

monitoring - linux inotify - 确定触发事件的用户

C# Threading.Suspend 已过时,线程已被弃用?

java - 对 servlet 请求调用 startAsync() 会在由 Jetty 9 的 ProxyServlet 终止的过滤器链中抛出 IllegalStateException

java - 解码时出现 JAXB 意外元素错误

java - 即使所有 CORS header 都存在,跨源资源共享问题

kubernetes - K8s : why is there no easy way to get notifications if a pod becomes unhealthy and is restarted?

Java TFS SDK - 如何获取特定提交中所做的更改

java - Java程序的内存消耗问题

delphi - 文件/文件夹监控

docker - 监控和重启Docker容器