0

我有一个非常慢的网络驱动器的问题,SimpleFileVisitor并且应该跳过它但也挂起。我想问题是读取文件属性。是否有一种内置方法可以只查看路径并忽略其他任何内容以尽可能快?

    final FileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
        private final PathMatcher matcherDirs = FileSystems.getDefault().getPathMatcher(PATTERN_DIRS);

        @Override
        public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
            if (!matcherDirs.matches(dir)) {
                logger.trace("skipping directory {}", dir.toString());
                return FileVisitResult.SKIP_SUBTREE;
            }

            logger.debug("visit directory {}", dir.toString());
            return FileVisitResult.CONTINUE;
        }

    };

创建的日志:

2021-06-10 10:10:08,137 | TRACE | skipping directory /ap/os/script
2021-06-10 10:10:08,137 | TRACE | skipping directory /at
2021-06-10 10:10:18,143 | TRACE | skipping directory /backups
2021-06-10 10:10:18,143 | TRACE | skipping directory /dev

仅列出目录很快,但查看它很慢。所以访问者也可能很快,恕我直言。

$ time ls -dl /backups
dr-xr-xr-x    1 root     system            1 Jan 06 15:54 /backups
real    0m0.005s
user    0m0.000s
sys     0m0.001s

$ time ls -l /backups 
total 0
real    0m30.015s
user    0m0.000s
sys     0m0.001s
4

0 回答 0