文件夹搜索Java

时间:2018-11-13 12:51:42

标签: java file search java-8 directory

我想找到每个具有特定名称的文件夹。我找到了解决方案,但它是次优的,因为它需要很长时间。

private List<Path> walkDirTreeAndSearch(final Path rootFolder, final String search, final boolean xoraya) {
    final List<Path> foundTraces = new ArrayList<>();
    long time;

    time = System.currentTimeMillis();

    if (!Files.exists(rootFolder)) {
        return foundTraces;
    }

    //Finds all files with the specific 'search' string
    try {
        Files.walk(Paths.get(String.valueOf(rootFolder)), FileVisitOption.FOLLOW_LINKS)
                .filter(t -> t.toString().contains(search)).forEach(foundTraces::add);
    } catch (final IOException e) {
        LOG.error(e);
    }

    //Finds all roots with 'xoraya' in it
    if (xoraya) {
        final Predicate<Path> pathPredicate2 = path -> path.getParent().toString().matches("^((?!xoraya).)*$");
        foundTraces.removeIf(pathPredicate2);
        final Predicate<Path> pathPredicate1 = path -> path.getParent().toString().contains("converted");
        foundTraces.removeIf(pathPredicate1);
    }

    time = System.currentTimeMillis() - time;

    return foundTraces; 
}

有人知道找到具有特定名称的每个文件夹的方法吗?

0 个答案:

没有答案