Java Examples for java.nio.file.Files.walkFileTree

The following java examples will help you to understand the usage of java.nio.file.Files.walkFileTree. These source code samples are taken from different open source projects.

Example 1
Project: craken-master  File: Files.java View source code
public static final void walkFileJDK7(File parent, final FileVisitor fvisitor) throws IOException {
    Path start = parent.toPath();
    java.nio.file.FileVisitor<? super Path> visitor = new SimpleFileVisitor<Path>() {

        public java.nio.file.FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            fvisitor.visitFile(file.toFile());
            return java.nio.file.FileVisitResult.CONTINUE;
        }
    };
    java.nio.file.Files.walkFileTree(start, visitor);
}
Example 2
Project: bonita-ui-designer-master  File: Zipper.java View source code
/**
     * Adds the contents of the given source directory to the zip, by accepting only directories and files accepted by the
     * given predicates.
     *
     * @param sourceDirectory          the source directory
     * @param directoryPredicate       the predicate used to accept directories
     * @param filePredicate            the predicate used to accept files
     * @param destinationDirectoryName the name of the target directory, in the zip, where all the files must be added.
     * @throws IOException
     */
public void addDirectoryToZip(final Path sourceDirectory, final PathPredicate directoryPredicate, final FilePredicate filePredicate, final String destinationDirectoryName) throws IOException {
    walkFileTree(sourceDirectory, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            if (!directoryPredicate.accept(dir)) {
                return FileVisitResult.SKIP_SUBTREE;
            }
            return super.preVisitDirectory(dir, attrs);
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (filePredicate.accept(file.toFile())) {
                addToZip(file, normalizeZipEntryName(get(destinationDirectoryName).resolve(sourceDirectory.relativize(file))));
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
Example 3
Project: gy-phonegap-build-master  File: FilesUtils.java View source code
public static void removeDirectory(Path path) throws IOException {
    java.nio.file.Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
            Files.deleteIfExists(file);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            if (exc == null) {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
            throw exc;
        }
    });
}
Example 4
Project: downlords-faf-client-master  File: FileUtils.java View source code
public static void deleteRecursively(Path path) throws IOException {
    walkFileTree(path, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
            if (e == null) {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            } else {
                throw e;
            }
        }
    });
}
Example 5
Project: mapr-spout-master  File: Utils.java View source code
/**
     * Recursive delete that handles symlinks and such correctly.  Note that the standard
     * recursive implementation isn't safe because it can't the difference between symlinks
     * and directories.
     *
     * @param f File or directory to recursively delete
     * @return The starting file
     * @throws IOException If a deletion fails.
     */
public static Path deleteRecursively(File f) throws IOException {
    return java.nio.file.Files.walkFileTree(f.toPath(), new FileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (attrs.isRegularFile()) {
                assertTrue(file.toFile().delete());
                return FileVisitResult.CONTINUE;
            } else if (attrs.isDirectory()) {
                return FileVisitResult.CONTINUE;
            } else {
                return FileVisitResult.SKIP_SUBTREE;
            }
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
            throw new UnsupportedOperationException("Default operation");
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            assertTrue(dir.toFile().delete());
            return FileVisitResult.CONTINUE;
        }
    });
}
Example 6
Project: commonj-master  File: DirectoryHasher.java View source code
public void hashDirectory(String directory) throws IOException {
    File dir = new File(directory);
    // check if the directory exists
    if (!dir.exists()) {
        throw new FileNotFoundException("Directory " + dir + " does not exist");
    }
    logger.info("Starting to walk {}", dir);
    java.nio.file.Files.walkFileTree(dir.toPath(), new DirectoryVisitor(filter));
}
Example 7
Project: kie-wb-common-master  File: AbstractVFSLookupManager.java View source code
public List<I> getItemsByPath(final org.uberfire.java.nio.file.Path root) {
    try {
        final List<I> result = new LinkedList<I>();
        if (ioService.exists(root)) {
            walkFileTree(checkNotNull("root", root), new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(final org.uberfire.java.nio.file.Path _file, final BasicFileAttributes attrs) throws IOException {
                    checkNotNull("file", _file);
                    checkNotNull("attrs", attrs);
                    final org.uberfire.backend.vfs.Path file = org.uberfire.backend.server.util.Paths.convert(_file);
                    if (acceptsPath(file)) {
                        I item = null;
                        try {
                            // portable diagram representation.
                            item = getItemByPath(file);
                        } catch (final Exception e) {
                            LOG.error("Cannot load diagram for path [" + file + "]", e);
                        }
                        if (null != item) {
                            result.add(item);
                        }
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }
        return result;
    } catch (Exception e) {
        LOG.error("Error while loading from VFS the item with path [" + root + "].", e);
    }
    return null;
}
Example 8
Project: swift-master  File: TestingUtils.java View source code
public static List<Path> listMatchingFiles(Path start, String glob) throws IOException {
    final ImmutableList.Builder<Path> list = ImmutableList.builder();
    final PathMatcher matcher = start.getFileSystem().getPathMatcher("glob:" + glob);
    walkFileTree(start, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (matcher.matches(file)) {
                list.add(file);
            }
            return FileVisitResult.CONTINUE;
        }
    });
    return list.build();
}
Example 9
Project: jdk7u-jdk-master  File: Nulls.java View source code
public static void main(String[] args) throws IOException {
    try {
        Files.walkFileTree(null, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
        });
        npeExpected();
    } catch (NullPointerException e) {
    }
    try {
        Files.walkFileTree(Paths.get("."), null, Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
        });
        npeExpected();
    } catch (NullPointerException e) {
    }
    try {
        Files.walkFileTree(Paths.get("."), EnumSet.noneOf(FileVisitOption.class), -1, new SimpleFileVisitor<Path>() {
        });
        throw new RuntimeException("IllegalArgumentExpected expected");
    } catch (IllegalArgumentException e) {
    }
    try {
        Set<FileVisitOption> opts = new HashSet<>(1);
        opts.add(null);
        Files.walkFileTree(Paths.get("."), opts, Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
        });
        npeExpected();
    } catch (NullPointerException e) {
    }
    try {
        Files.walkFileTree(Paths.get("."), EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, null);
        npeExpected();
    } catch (NullPointerException e) {
    }
    SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
    };
    boolean ranTheGauntlet = false;
    Path dir = Paths.get(".");
    BasicFileAttributes attrs = Files.readAttributes(dir, BasicFileAttributes.class);
    try {
        visitor.preVisitDirectory(null, attrs);
    } catch (NullPointerException x0) {
        try {
            visitor.preVisitDirectory(dir, null);
        } catch (NullPointerException x1) {
            try {
                visitor.visitFile(null, attrs);
            } catch (NullPointerException x2) {
                try {
                    visitor.visitFile(dir, null);
                } catch (NullPointerException x3) {
                    try {
                        visitor.visitFileFailed(null, new IOException());
                    } catch (NullPointerException x4) {
                        try {
                            visitor.visitFileFailed(dir, null);
                        } catch (NullPointerException x5) {
                            try {
                                visitor.postVisitDirectory(null, new IOException());
                            } catch (NullPointerException x6) {
                                ranTheGauntlet = true;
                            }
                        }
                    }
                }
            }
        }
    }
    if (!ranTheGauntlet)
        throw new RuntimeException("A visit method did not throw NPE");
}
Example 10
Project: che-master  File: FileTreeWalker.java View source code
@ScheduleRate(period = 10)
void walk() {
    try {
        LOG.debug("Tree walk started");
        Set<Path> deletedFiles = files.keySet().stream().filter( it -> !exists(it)).collect(toSet());
        fileDeleteConsumers.forEach(deletedFiles::forEach);
        files.keySet().removeAll(deletedFiles);
        Set<Path> deletedDirectories = directories.keySet().stream().filter( it -> !exists(it)).collect(toSet());
        directoryDeleteConsumers.forEach(deletedDirectories::forEach);
        directories.keySet().removeAll(deletedDirectories);
        walkFileTree(root.toPath(), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                for (PathMatcher matcher : directoryExcludes) {
                    if (matcher.matches(dir)) {
                        return SKIP_SUBTREE;
                    }
                }
                updateFsTreeAndAcceptConsumables(directories, directoryUpdateConsumers, directoryCreateConsumers, dir, attrs);
                return CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                for (PathMatcher matcher : fileExcludes) {
                    if (matcher.matches(file)) {
                        return CONTINUE;
                    }
                }
                updateFsTreeAndAcceptConsumables(files, fileUpdateConsumers, fileCreateConsumers, file, attrs);
                return CONTINUE;
            }
        });
        LOG.debug("Tree walk finished");
    } catch (NoSuchFileException e) {
        LOG.debug("Trying to process a file, however seems like it is already not present: {}", e.getMessage());
    } catch (Exception e) {
        LOG.error("Error while walking file tree", e);
    }
}
Example 11
Project: uberfire-master  File: WorkbenchServicesImpl.java View source code
@Override
public Set<PerspectiveDefinition> loadPerspectives() {
    final Set<PerspectiveDefinition> result = new HashSet<PerspectiveDefinition>();
    final Path perspectivesPath = userServices.buildPath("perspectives");
    if (ioService.exists(perspectivesPath)) {
        walkFileTree(perspectivesPath, new SimpleFileVisitor<Path>() {

            public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
                try {
                    checkNotNull("file", file);
                    checkNotNull("attrs", attrs);
                    String fileName = file.getFileName().toString();
                    if (fileName.endsWith(PERSPECTIVE_EXTENSION) && attrs.isRegularFile()) {
                        String perspectiveName = fileName.substring(0, fileName.indexOf(PERSPECTIVE_EXTENSION));
                        PerspectiveDefinition def = loadPerspective(perspectiveName);
                        if (def != null) {
                            result.add(def);
                        }
                    }
                } catch (final Exception ex) {
                    return FileVisitResult.TERMINATE;
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }
    return result;
}