Example usage for java.nio.file Files walkFileTree

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

Introduction

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

Prototype

public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor) throws IOException 

Source Link

Document

Walks a file tree.

Usage

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   w  ww . j ava  2 s  . com*/
        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 {/*  w  ww.j  a v a2s  .c o 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:org.cryptomator.cryptofs.CryptoFileSystemProviderIntegrationTest.java

@After
public void teardown() throws IOException {
    Files.walkFileTree(tmpPath, new DeletingFileVisitor());
}

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/*from w w w .  jav a  2  s. 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/*  ww w.j  a va 2  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: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 {/*from  w  ww.j ava2s .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:com.netease.hearttouch.hthotfix.HashTransformExecutor.java

public void transformDirectory(File inputFile, File outputFile) throws IOException {

    Path inputDir = inputFile.toPath();
    Path outputDir = outputFile.toPath();

    if (!Files.isDirectory(inputDir)) {
        return;//w ww.  j  av a2 s .c o  m
    }
    final OutputDirectory outputDirectory = new OutputDirectory(outputDir);
    Files.walkFileTree(inputDir, new ClasspathVisitor() {
        @Override
        protected void visitClass(Path path, byte[] bytecode) throws IOException {
            ClassReader cr = new ClassReader(bytecode);
            String className = cr.getClassName();
            if (!extension.getGeneratePatch()) {
                hashFileGenerator.addClass(bytecode);
                outputDirectory.writeClass(className, hackInjector.inject(className, bytecode));
            } else {
                outputDirectory.writeClass(className, bytecode);
            }
        }

        @Override
        protected void visitResource(Path relativePath, byte[] content) throws IOException {
            outputDirectory.writeFile(relativePath, content);
        }
    });
}

From source file:util.ManageResults.java

/**
 * // w  w w. ja v  a 2  s .c  o m
 * @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;
}

From source file:org.eclipse.vorto.codegen.api.CopyResourceTask.java

public void generate(Context metaData, IMappingContext mappingContext, final IGeneratedWriter outputter) {
    try {/*from www.  j  av a 2s.c o m*/
        Path start = Paths.get(basePath.toURI());
        Files.walkFileTree(start, 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 {
                outputter.write(new Generated(file.getFileName().toFile().getName(),
                        getOutputPath(file).isEmpty() ? null : getOutputPath(file),
                        FileUtils.readFileToByteArray((file.toFile()))));
                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;
            }
        });
    } catch (IOException ioEx) {
        throw new RuntimeException(ioEx);
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.netease.hearttouch.hthotfix.patch.PatchRefGeneratorTransformExecutor.java

public void transformDirectory(File inputFile, File outputFile) throws IOException {

    Path inputDir = inputFile.toPath();
    Path outputDir = outputFile.toPath();

    if (!Files.isDirectory(inputDir)) {
        return;//from ww w  .  j a v  a 2  s  .co  m
    }
    final OutputDirectory outputDirectory = new OutputDirectory(outputDir);
    Files.walkFileTree(inputDir, new ClasspathVisitor() {
        @Override
        protected void visitClass(Path path, byte[] bytecode) throws IOException {
            ClassReader cr = new ClassReader(bytecode);
            String className = cr.getClassName();
            if (extension.getGeneratePatch() && extension.getScanRef()) {
                if (!refScanInstrument.isPatchClass(className.replace("/", "."))) {
                    boolean bPatchRef = refScanInstrument.hasReference(bytecode, project);
                    if (bPatchRef) {
                        patchJarHelper.writeClassToDirectory(className, bytecode);
                    }
                }
            }
            outputDirectory.writeClass(className, bytecode);
        }

        @Override
        protected void visitResource(Path relativePath, byte[] content) throws IOException {
            outputDirectory.writeFile(relativePath, content);

        }
    });
}