Example usage for java.nio.file Files find

List of usage examples for java.nio.file Files find

Introduction

In this page you can find the example usage for java.nio.file Files find.

Prototype

public static Stream<Path> find(Path start, int maxDepth, BiPredicate<Path, BasicFileAttributes> matcher,
        FileVisitOption... options) throws IOException 

Source Link

Document

Return a Stream that is lazily populated with Path by searching for files in a file tree rooted at a given starting file.

Usage

From source file:gedi.util.FileUtils.java

public static String[] find(String path, String glob, boolean stripPath) throws IOException {
    if (!path.endsWith("/"))
        path = path + "/";
    String cpath = path;/*  w w  w. jav a 2  s  .c om*/
    ArrayList<String> re = new ArrayList<String>();
    PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + path + glob);
    Files.find(Paths.get(path), 9999, (p, per) -> {
        return matcher.matches(p);
    }, FileVisitOption.FOLLOW_LINKS).forEach(f -> {
        String file = f.toString();
        if (stripPath && file.startsWith(cpath))
            file = file.substring(cpath.length());
        re.add(file);
    });
    return re.toArray(new String[0]);
}

From source file:org.omegat.util.FileUtil.java

/**
 * Returns a list of all files under the root directory by absolute path.
 * //  w  w w . j av  a 2s. c om
 * @throws IOException
 */
public static List<File> buildFileList(File rootDir, boolean recursive) throws IOException {
    int depth = recursive ? Integer.MAX_VALUE : 0;
    try (Stream<Path> stream = Files.find(rootDir.toPath(), depth, (p, attr) -> p.toFile().isFile(),
            FileVisitOption.FOLLOW_LINKS)) {
        return stream.map(Path::toFile).sorted(StreamUtil.localeComparator(File::getPath))
                .collect(Collectors.toList());
    }
}

From source file:org.omegat.util.FileUtil.java

public static List<String> buildRelativeFilesList(File rootDir, List<String> includes, List<String> excludes)
        throws IOException {
    Path root = rootDir.toPath();
    Pattern[] includeMasks = FileUtil.compileFileMasks(includes);
    Pattern[] excludeMasks = FileUtil.compileFileMasks(excludes);
    BiPredicate<Path, BasicFileAttributes> pred = (p, attr) -> {
        return p.toFile().isFile()
                && FileUtil.checkFileInclude(root.relativize(p).toString(), includeMasks, excludeMasks);
    };/*  w  ww.j  a v a 2  s.co m*/
    try (Stream<Path> stream = Files.find(root, Integer.MAX_VALUE, pred, FileVisitOption.FOLLOW_LINKS)) {
        return stream.map(p -> root.relativize(p).toString().replace('\\', '/'))
                .sorted(StreamUtil.localeComparator(Function.identity())).collect(Collectors.toList());
    }
}