Example usage for java.nio.file FileVisitResult CONTINUE

List of usage examples for java.nio.file FileVisitResult CONTINUE

Introduction

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

Prototype

FileVisitResult CONTINUE

To view the source code for java.nio.file FileVisitResult CONTINUE.

Click Source Link

Document

Continue.

Usage

From source file:de.alexkamp.sandbox.ChrootSandboxFactory.java

@Override
public void deleteSandbox(final SandboxData sandbox) {
    File copyDir = sandbox.getBaseDir();

    try {/*from   www. j av a2 s .co m*/
        final AtomicInteger depth = new AtomicInteger(0);
        Files.walkFileTree(copyDir.toPath(), new FileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes basicFileAttributes)
                    throws IOException {
                int d = depth.getAndIncrement();

                // see whether the mounts are gone
                if (1 == d) {
                    for (Mount m : sandbox) {
                        if (path.endsWith(m.getMountPoint().substring(1))) {
                            if (0 != path.toFile().listFiles().length) {
                                throw new IllegalArgumentException(
                                        path.getFileName() + " has not been unmounted.");
                            }
                        }
                    }
                }

                return FileVisitResult.CONTINUE;
            }

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

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

            @Override
            public FileVisitResult postVisitDirectory(Path path, IOException e) throws IOException {
                Files.delete(path);
                depth.decrementAndGet();
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        throw new SandboxException(e);
    }
}

From source file:uk.co.unclealex.executable.generator.CodeGeneratorImplTest.java

@Test
public void testCodeGeneration() throws IOException, ExecutableScanException {
    CodeGenerator codeGenerator = new CodeGeneratorImpl(new MockAllClassNamesCollector(),
            new MockExecutableAnnotationInformationFinder(), new MockGeneratedClassWriter());
    codeGenerator.generate(getClass().getClassLoader(), tempDir.resolve("nothing"), tempDir);
    final SortedSet<String> actualPathNames = Sets.newTreeSet();
    FileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
        @Override/*from w  w w .ja v a  2s  .c  o  m*/
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (Files.isDirectory(file)) {
                Assert.fail("Found directory " + file + " when no directories were expected.");
            }
            actualPathNames.add(file.getFileName().toString());
            return FileVisitResult.CONTINUE;
        }
    };
    Files.walkFileTree(tempDir, visitor);
    Assert.assertArrayEquals("The wrong class files were written.",
            new String[] { "uk.co.unclealex.executable.generator.TestOne.txt",
                    "uk.co.unclealex.executable.generator.TestTwo.txt" },
            Iterables.toArray(actualPathNames, String.class));
    checkContents("uk.co.unclealex.executable.generator.TestOne");
    checkContents("uk.co.unclealex.executable.generator.TestTwo");
}

From source file:it.polimi.diceH2020.SPACE4CloudWS.fileManagement.FileUtility.java

public boolean destroyDir(@NotNull File path) throws IOException {
    Path directory = path.toPath();

    @Getter//from   w  w  w  . j a v a  2 s.  c o m
    @Setter
    class BooleanWrapper {
        boolean deleted;
    }
    BooleanWrapper status = new BooleanWrapper();

    Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            status.setDeleted(policy.delete(file.toFile()));
            return FileVisitResult.CONTINUE;
        }

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

    return status.isDeleted();
}

From source file:org.bonitasoft.web.designer.controller.export.ZipperTest.java

private void expectSameDirContent(final Path actual, final Path expected) throws IOException {
    Files.walkFileTree(actual, new SimpleFileVisitor<Path>() {

        @Override/* www  .ja  v a  2s . com*/
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            Path expectedFile = expected.resolve(actual.relativize(file));
            assertThat(expectedFile.toFile()).exists();
            assertThat(expectedFile.toFile()).hasContent(new String(Files.readAllBytes(expectedFile)));
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            Path expectedDir = expected.resolve(actual.relativize(dir));
            assertThat(expectedDir.toFile()).exists();
            return FileVisitResult.CONTINUE;
        }

    });
}

From source file:br.com.uol.runas.classloader.JarClassLoader.java

private void addUrlsFromDir(URL url) throws IOException, URISyntaxException {
    addURL(url);//www  . java  2 s.  c  o  m

    Files.walkFileTree(Paths.get(url.toURI()), new FileVisitor<Path>() {
        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            addURL(dir.toUri().toURL());
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            addURL(file.toUri().toURL());
            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;
        }
    });
}

From source file:de.ingrid.interfaces.csw.tools.FileUtils.java

/**
 * Delete a file or directory specified by a {@link Path}. This method uses
 * the new {@link Files} API and allows to specify a regular expression to
 * remove only files that match that expression.
 * /*w w w  . j a  v a 2s .  c o m*/
 * @param path
 * @param pattern
 * @throws IOException
 */
public static void deleteRecursive(Path path, final String pattern) throws IOException {

    final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("regex:" + pattern);

    if (!Files.exists(path))
        return;

    Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (pattern != null && matcher.matches(file.getFileName())) {
                Files.delete(file);
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
            // try to delete the file anyway, even if its attributes
            // could not be read, since delete-only access is
            // theoretically possible
            if (pattern != null && matcher.matches(file.getFileName())) {
                Files.delete(file);
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            if (exc == null) {
                if (matcher.matches(dir.getFileName())) {
                    if (dir.toFile().list().length > 0) {
                        // remove even if not empty
                        FileUtils.deleteRecursive(dir);
                    } else {
                        Files.delete(dir);
                    }
                }
                return FileVisitResult.CONTINUE;
            } else {
                // directory iteration failed; propagate exception
                throw exc;
            }
        }

    });
}

From source file:org.structr.web.maintenance.deploy.FileImportVisitor.java

@Override
public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {

    if (!basePath.equals(dir)) {
        createFolder(dir);/*from  w w  w. j a v  a  2s  .  c o m*/
    }

    return FileVisitResult.CONTINUE;
}

From source file:uk.nhs.fhir.util.FhirFileUtils.java

public static void deleteRecursive(Path f) {
    try {/*w w w.j  a v a 2  s  .  co m*/
        Files.walkFileTree(f, 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 {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        logger.error("Caught exception trying to delete " + f.toString() + ".", e);
    }
}

From source file:org.structr.web.maintenance.deploy.ComponentImportVisitor.java

@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {

    if (attrs.isRegularFile()) {

        final String fileName = file.getFileName().toString();
        if (fileName.endsWith(".html")) {

            try {

                createComponent(file, fileName);

            } catch (FrameworkException fex) {
                logger.warn("Exception while importing shared component {}: {}", fileName, fex.toString());
            }//ww w  .  j a va 2 s.  c o m
        }

    } else {

        logger.warn("Unexpected directory {} found in components/ directory, ignoring",
                file.getFileName().toString());
    }

    return FileVisitResult.CONTINUE;
}

From source file:illarion.compile.Compiler.java

private static void processFileMode(@Nonnull final CommandLine cmd) throws IOException {
    storagePaths = new EnumMap<>(CompilerType.class);
    String npcPath = cmd.getOptionValue('n');
    if (npcPath != null) {
        storagePaths.put(CompilerType.easyNPC, Paths.get(npcPath));
    }/*  w  w  w . ja  va2s.co  m*/
    String questPath = cmd.getOptionValue('q');
    if (questPath != null) {
        storagePaths.put(CompilerType.easyQuest, Paths.get(questPath));
    }

    for (String file : cmd.getArgs()) {
        Path path = Paths.get(file);
        if (Files.isDirectory(path)) {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    FileVisitResult result = super.visitFile(file, attrs);
                    if (result == FileVisitResult.CONTINUE) {
                        processPath(file);
                        return FileVisitResult.CONTINUE;
                    }
                    return result;
                }
            });
        } else {
            processPath(path);
        }
    }
}