首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WatchService排除文件夹

WatchService排除文件夹
EN

Stack Overflow用户
提问于 2017-10-12 08:51:40
回答 1查看 693关注 0票数 2

我正试图查看文件夹中的更改。此文件夹包含我不想观看的子文件夹。不幸的是,WatchService将这些子文件夹中的更改通知我。我想这是因为这些文件夹的最后一个更改日期更新了。

所以我试着把它们排出来:

代码语言:javascript
复制
WatchService service = FileSystems.getDefault().newWatchService();
workPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
            StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
try {
    WatchKey watchKey = watchService.take();
    for (WatchEvent<?> event : watchKey.pollEvents()) {
        @SuppressWarnings("unchecked")
        WatchEvent<Path> ev = (WatchEvent<Path>) event;
        Path fileName = ev.context();
        if (!Files.isDirectory(fileName)) {
            logger.log(LogLevel.DEBUG, this.getClass().getSimpleName(),
                    "Change registered in " + fileName + " directory. Checking configurations.");
            /* do stuff */
        }
    }
    if (!watchKey.reset()) {
        break;
    }
} catch (InterruptedException e) {
    return;
}

但这不管用。上下文的结果路径是相对的,Files.isDirectory()无法确定它是目录还是文件。

有排除子文件夹的方法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-12 09:32:23

您可以尝试下面的代码片段。为了获得完整的路径,需要调用解析()函数

代码语言:javascript
复制
Map<WatchKey, Path> keys = new HashMap<>();

    try {
        Path path = Paths.get("<directory u want to watch>");
        FileSystem fileSystem = path.getFileSystem();
        WatchService service = fileSystem.newWatchService();

        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    if (<directory you want to exclude>) {
                            return FileVisitResult.SKIP_SUBTREE;
                    }

                    WatchKey key = dir.register(service, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
                    keys.put(key, dir);
                    return FileVisitResult.CONTINUE;
                }
        });

        WatchKey key = null;
        while (true) {
            key = service.take();
            while (key != null) {
                WatchEvent.Kind<?> kind;
                for (WatchEvent<?> watchEvent : key.pollEvents()) {
                    kind = watchEvent.kind();
                    if (OVERFLOW == kind) {
                        continue;
                    }

                    Path filePath = ((WatchEvent<Path>) watchEvent).context();
                    Path absolutePath = keys.get(key).resolve(filePath);

                    if (kind == ENTRY_CREATE) {
                        if (Files.isDirectory(absolutePath, LinkOption.NOFOLLOW_LINKS)) {
                            WatchKey newDirKey = absolutePath.register(service, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
                            keys.put(newDirKey, absolutePath);
                        }
                    }

                }
                if (!key.reset()) {
                    break; // loop
                }
            }
        }
    } catch (Exception ex) {
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46705276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档