Java Nio WatchService用于多个目录 [英] Java nio WatchService for multiple directories

查看:437
本文介绍了Java Nio WatchService用于多个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Java NIO监视(监视)多个目录 WatchService . 我的问题是要监视的目录数量是动态的,用户可以向WatchService添加任意数量的目录.这可以实现吗?

I want to watch (monitor) multiple directories using Java NIO WatchService. My problem here is the number of directories to watch is dynamic and the user can add any number of directories to the WatchService. Is this achievable?

推荐答案

可以使用同一WatchService注册多个路径.每个路径都有自己的WatchKey. take()poll()然后将返回与已修改路径相对应的WatchKey.

It is possible to register multiple paths with the same WatchService. Each path gets its own WatchKey. The take() or poll() will then return the WatchKey corresponding to the path that was modified.

有关详细信息,请参见 Java的WatchDir示例.

See Java's WatchDir example for details.

这篇关于Java Nio WatchService用于多个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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