我的操作系统是Windows7。
我创建的目录是这样的
WatchService watchService = FileSystems.getDefault().newWatchService();
Path rootPath = Paths.createDirectory(Paths.get("c:/foo"));
rootPath.register(watchService, ENTRY_MODIFY, ENTRY_CREATE);
Path depth1Child = Paths.createDirectory(Paths.get("c:/foo/depth1"));
WatchKey key = null;
//printed : ENTRY_CREATE c:/foo/depth1
while((key=watchService.poll()) != null) {
for (WatchEvent e : key.pollEvents())
System.out.println(e.kind()+" "+e.context());
key.reset();
}
depth1Child.register(watchService, ENTRY_MODIFY, ENTRY_CREATE);
Path depth2Child = Paths.createDirectory(Paths.get("c:/foo/depth1/depth2"));
//printed : ENTRY_MODIFY c:/foo/depth1, ENTRY_CREATE c:/foo/depth1/depth2
while((key=watchService.poll()) != null) {
for (WatchEvent e : key.pollEvents())
System.out.println(e.kind()+" "+e.context());
key.reset();
}为什么在第一次深度目录创建和第二次路径创建后发生不同的事件?
我在ENTRY_MODIFY和ENTRY_CREATE中注册了c:/foo path,c:/foo/depth1、c:/foo/depth2也是如此。
谢谢你的帮忙
发布于 2015-12-25 06:12:52
由于为ENTRY_MODIFY和ENTRY_CREATE事件注册了"c:/foo“,因此当您创建"c:/foo/depth1”时,您将获得ENTRY_CREATE事件。因为为“c:/foo/depth1”注册了ENTRY_MODIFY和ENTRY_CREATE事件,所以当您创建"c:/foo/depth1/depth2“时,您已经修改了"c:/foo/depth1",所以您得到:
适用于"c:/foo/depth1"
https://stackoverflow.com/questions/34043661
复制相似问题