Example usage for java.nio.file Files walk

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

Introduction

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

Prototype

public static Stream<Path> walk(Path start, int maxDepth, FileVisitOption... options) throws IOException 

Source Link

Document

Return a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file.

Usage

From source file:org.mitre.mpf.mvc.util.NIOUtils.java

public static long getSubFilesAndDirsCount(final Path p) {
    long count = 0;
    //help from http://stackoverflow.com/questions/18300105/number-of-subfolders-in-a-folder-directory
    try {//from www .ja v  a 2  s .  co  m
        count = Files.walk(p, 1, FileVisitOption.FOLLOW_LINKS).count() - 1; // '-1' because the Path param (p) is also counted in
    } catch (IOException e) {
        log.error("Error determing the count of sub files and directories.", e);
    }

    return count;
}

From source file:jonelo.jacksum.concurrent.Jacksum2Cli.java

private void loadFilesToHash(List<Path> allFiles, Map<Path, Long> fileSizes, Map<Path, Long> fileLastModified)
        throws IOException {

    final int maxDepth = this.recursive ? Integer.MAX_VALUE : 1;
    final FileSystem fs = FileSystems.getDefault();
    final FileVisitOption[] options = this.ignoreSymbolicLinksToDirectories ? new FileVisitOption[0]
            : new FileVisitOption[] { FileVisitOption.FOLLOW_LINKS };

    for (String filename : this.filenames) {
        Files.walk(fs.getPath(filename), maxDepth, options).filter(path -> Files.isRegularFile(path))
                .forEach(path -> {//ww  w.  ja  v  a  2 s . c o  m
                    allFiles.add(path);
                    try {
                        fileSizes.put(path, Files.size(path));
                        fileLastModified.put(path, Files.getLastModifiedTime(path).toMillis());
                    } catch (IOException ioEx) {
                        fileSizes.put(path, -1l);
                        fileLastModified.put(path, 0l);
                    }
                });
    }

}