2

我可以制作多少个 newWatchService ?

try{
    for(Path path : PathList) {
        watcher = path.getFileSystem().newWatchService();
    } catch (IOException e) {
        log.error(e);
    }
}

--> 结果:IOExeption:打开的文件太多...

4

1 回答 1

0

我认为您应该只创建一个观察者服务,但要注册 [m] 任何路径。

根据 Oracle 文档 ( https://docs.oracle.com/javase/tutorial/essential/io/walk.html ) 给出的示例,只创建了一个监视服务,作为 WatchDir 类的成员变量。注意“this.watcher”

public class WatchDir {

    private final WatchService watcher;

班上的其他地方……

 /**
 * Creates a WatchService and registers the given directory
  */
WatchDir(Path dir, boolean recursive) throws IOException {
    this.watcher = FileSystems.getDefault().newWatchService();

相同的服务用于递归地注册给定文件夹内的所有路径。

最后,注册发生在这里......

/**
 * Register the given directory with the WatchService
 */
private void register(Path dir) throws IOException {
    WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
于 2015-07-20T18:17:45.023 回答