Example usage for java.nio.file SimpleFileVisitor SimpleFileVisitor

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

Introduction

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

Prototype

protected SimpleFileVisitor() 

Source Link

Document

Initializes a new instance of this class.

Usage

From source file:jobs.ExportDatabaseToFilesystem.java

public void doJob() throws Exception {
    Path p = PathService.getRootImageDirectory();
    Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
        @Override//from w w w .jav a2s.  com
        public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
            if (PathService.isImage(path)) {
                DatabaseImage dbi = DatabaseImage.forPath(path);
                ImportExportService.exportData(dbi);
            }
            return FileVisitResult.CONTINUE;
        }
    });
}

From source file:org.apdplat.superword.extract.PhraseExtractor.java

public static Set<String> parseDir(String dir) {
    Set<String> data = new HashSet<>();
    LOGGER.info("?" + dir);
    try {// w w w.  j av  a  2s  .  co m
        Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                data.addAll(parseFile(file.toFile().getAbsolutePath()));
                return FileVisitResult.CONTINUE;
            }

        });
    } catch (IOException e) {
        LOGGER.error("?", e);
    }
    return data;
}

From source file:ch.bender.evacuate.Helper.java

/**
 * Deletes a whole directory (recursively)
 * <p>/*w  w  w.  ja v  a 2 s .  c o m*/
 * @param aDir
 *        a folder to be deleted (must not be null)
 * @throws IOException
 */
public static void deleteDirRecursive(Path aDir) throws IOException {
    if (aDir == null) {
        throw new IllegalArgumentException("aDir must not be null");
    }

    if (Files.notExists(aDir)) {
        return;
    }

    if (!Files.isDirectory(aDir)) {
        throw new IllegalArgumentException("given aDir is not a directory");
    }

    Files.walkFileTree(aDir, new SimpleFileVisitor<Path>() {
        /**
         * @see java.nio.file.SimpleFileVisitor#visitFileFailed(java.lang.Object, java.io.IOException)
         */
        @Override
        public FileVisitResult visitFileFailed(Path aFile, IOException aExc) throws IOException {
            if ("System Volume Information".equals((aFile.getFileName().toString()))) {
                return FileVisitResult.SKIP_SUBTREE;
            }

            throw aExc;
        }

        /**
         * @see java.nio.file.SimpleFileVisitor#preVisitDirectory(java.lang.Object, java.nio.file.attribute.BasicFileAttributes)
         */
        @Override
        public FileVisitResult preVisitDirectory(Path aFile, BasicFileAttributes aAttrs) throws IOException {
            if ("System Volume Information".equals((aFile.getFileName()))) {
                return FileVisitResult.SKIP_SUBTREE;
            }

            return FileVisitResult.CONTINUE;
        }

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

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            if (dir.isAbsolute() && dir.getRoot().equals(dir)) {
                myLog.debug("root cannot be deleted: " + dir.toString());
                return FileVisitResult.CONTINUE;
            }

            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }

    });
}

From source file:org.apdplat.superword.extract.DefinitionExtractor.java

public static Set<Word> parseDir(String dir) {
    Set<Word> data = new HashSet<>();
    LOGGER.info("?" + dir);
    try {//from ww  w .j  a  v a  2 s  . com
        Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                data.addAll(parseFile(file.toFile().getAbsolutePath()));
                return FileVisitResult.CONTINUE;
            }

        });
    } catch (IOException e) {
        LOGGER.error("?", e);
    }
    return data;
}

From source file:org.wildfly.plugin.server.Archives.java

/**
 * Recursively deletes a directory. If the directory does not exist it's ignored.
 *
 * @param dir the directory//w w w . j av  a 2s  . co m
 *
 * @throws java.lang.IllegalArgumentException if the argument is not a directory
 */
public static void deleteDirectory(final Path dir) throws IOException {
    if (Files.notExists(dir))
        return;
    if (!Files.isDirectory(dir)) {
        throw new IllegalArgumentException(String.format("Path '%s' is not a directory.", dir));
    }
    Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            Files.delete(file);
            return CONTINUE;
        }

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

From source file:net.sourceforge.pmd.docs.RuleDocGeneratorTest.java

@After
public void cleanup() throws IOException {
    Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
        @Override/*from ww  w  . j  a  va2 s .  c o  m*/
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
        }

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

From source file:org.global.canvas.services.impl.StorageServiceImpl.java

@PostConstruct
public void init() throws IOException {

    Path directory = Paths.get("/tmp/canvas/");
    Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
        @Override/*from   ww w .j  a  va  2s  . co  m*/
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            Files.delete(file);
            return FileVisitResult.CONTINUE;
        }

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

    });
    System.out.println("Dir deleted.. let's create it now..");
    File dir = new File("/tmp/canvas/");
    boolean created = dir.mkdir();

}

From source file:org.apdplat.superword.extract.SynonymAntonymExtractor.java

public static Set<SynonymAntonym> parseDir(String dir) {
    Set<SynonymAntonym> data = new HashSet<>();
    LOGGER.info("?" + dir);
    try {/*www.  j  a v a 2  s .  c  om*/
        Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                data.addAll(parseFile(file.toFile().getAbsolutePath()));
                return FileVisitResult.CONTINUE;
            }

        });
    } catch (IOException e) {
        LOGGER.error("?", e);
    }
    return data;
}

From source file:energy.usef.environment.tool.util.FileUtil.java

public static void removeFolders(String folderName) throws IOException {
    if (folderName == null || folderName.isEmpty() || folderName.trim().equals("\\")
            || folderName.trim().equals("/")) {
        LOGGER.warn("Prevent to delete folders recursively from the root location.");
    } else {// w  ww . j a  v  a2 s  .c o  m
        Path directory = Paths.get(folderName);
        Files.walkFileTree(directory, 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 exc) throws IOException {
                LOGGER.debug("Removing folder " + dir);
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    }
}

From source file:util.ManageResults.java

/**
 * //from  ww w  . ja va 2s.com
 * @param instances
 * @param algorithms
 * @return the paths containing the results based on the instances and algorithms passed
 */
static List<Path> getPaths(List<String> instances, List<String> algorithms) {
    final List<Path> paths = new ArrayList<>();
    for (String instance : instances) {
        for (String algorithm : algorithms) {
            String path = "experiment/" + instance + "/" + algorithm;

            try {
                Path startPath = Paths.get(path);
                Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
                    @Override
                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
                        //System.out.println("Dir: " + dir.toString());
                        return FileVisitResult.CONTINUE;
                    }

                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                        //System.out.println("File: " + file.toString());
                        if (!file.getFileName().toString().contains("KruskalWallisResults")
                                && !file.getFileName().toString().contains("Hypervolume_Results")) {
                            Path currentPath = file.getParent();

                            if (!paths.contains(currentPath)) {
                                paths.add(currentPath);
                            }
                        }
                        return FileVisitResult.CONTINUE;
                    }

                    @Override
                    public FileVisitResult visitFileFailed(Path file, IOException e) {
                        return FileVisitResult.CONTINUE;
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return paths;
}