Example usage for java.nio.file FileVisitor FileVisitor

List of usage examples for java.nio.file FileVisitor FileVisitor

Introduction

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

Prototype

FileVisitor

Source Link

Usage

From source file:org.schedulesdirect.grabber.Grabber.java

private void removeUnusedPrograms(FileSystem vfs) throws IOException {
    final int[] i = new int[] { 0 };
    final Path root = vfs.getPath("programs");
    Files.walkFileTree(root, new FileVisitor<Path>() {

        @Override// ww w .  j  ava  2  s.  com
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            return !Files.isSameFile(root, dir) ? FileVisitResult.SKIP_SUBTREE : FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            String id = file.getName(file.getNameCount() - 1).toString();
            id = id.substring(0, id.indexOf('.'));
            if (!activeProgIds.contains(id)) {
                if (LOG.isDebugEnabled())
                    LOG.debug(String.format("CacheCleaner: Unused '%s'", id));
                Files.delete(file);
                ++i[0];
            }
            return FileVisitResult.CONTINUE;
        }

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

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            return FileVisitResult.CONTINUE;
        }

    });
    LOG.info(String.format("Removed %d unused program(s).", i[0]));
}

From source file:org.schedulesdirect.grabber.Grabber.java

private void removeIgnoredStations(FileSystem vfs) throws IOException {
    final int[] i = new int[] { 0 };
    final Path root = vfs.getPath("schedules");
    Files.walkFileTree(root, new FileVisitor<Path>() {

        @Override//from  w  ww  .j  a v  a 2s .c  om
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            return !Files.isSameFile(root, dir) ? FileVisitResult.SKIP_SUBTREE : FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            String id = file.getName(file.getNameCount() - 1).toString();
            id = id.substring(0, id.indexOf('.'));
            if (stationList != null && !stationList.contains(id)) {
                if (LOG.isDebugEnabled())
                    LOG.debug(String.format("CacheCleaner: Remove '%s'", id));
                Files.delete(file);
                ++i[0];
            }
            return FileVisitResult.CONTINUE;
        }

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

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            return FileVisitResult.CONTINUE;
        }

    });
    LOG.info(String.format("Removed %d ignored station(s).", i[0]));
}

From source file:org.tinymediamanager.core.Utils.java

/**
 * Deletes a complete directory recursively, using Java NIO
 * //from  w ww  .  j a v a 2  s .  c om
 * @param dir
 *          directory to delete
 * @throws IOException
 */
public static void deleteDirectoryRecursive(Path dir) throws IOException {
    if (!Files.exists(dir)) {
        return;
    }

    LOGGER.info("Deleting complete directory: " + dir);
    Files.walkFileTree(dir, new FileVisitor<Path>() {

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }

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

        @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 {
            LOGGER.warn("Could not delete " + file + "; " + exc.getMessage());
            return FileVisitResult.CONTINUE;
        }

    });
}