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:sh.isaac.pombuilder.FileUtil.java

/**
 * Recursive delete.// ww  w .j a v a  2  s. c  o m
 *
 * @param file the file
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void recursiveDelete(File file) throws IOException {
    if ((file == null) || !file.exists()) {
        return;
    }

    Files.walkFileTree(file.toPath(), 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;
        }
    });
    file.delete();
}

From source file:com.preferanser.server.client.DealUploader.java

private List<File> discoverJsonFiles(String jsonDealsPath) throws URISyntaxException, IOException {
    final List<File> jsonFiles = Lists.newArrayList();
    Path path = Paths.get(DealUploader.class.getResource(jsonDealsPath).toURI());
    Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
        @Override/*from  w ww  . jav  a 2 s . co m*/
        public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
            File file = path.toFile();
            if (attrs.isRegularFile() && file.getName().endsWith(".json"))
                jsonFiles.add(file);
            return super.visitFile(path, attrs);
        }
    });
    return jsonFiles;
}

From source file:org.abondar.experimental.eventsearch.SearchData.java

public void indexDocs(final IndexWriter iw, Path path) throws IOException {

    if (Files.isDirectory(path)) {
        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
            @Override//from ww  w  .jav a  2s.c o m
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                try {
                    indexDoc(iw, file, attrs.lastModifiedTime().toMillis());
                } catch (IOException ignore) {
                    // don't index files that can't be read.
                }
                return FileVisitResult.CONTINUE;
            }

        });
    } else {

        indexDoc(iw, path, Files.getLastModifiedTime(path).toMillis());
    }
}

From source file:com.wandoulabs.jodis.TestRoundRobinJedisPool.java

private void deleteDirectory(File directory) throws IOException {
    if (!directory.exists()) {
        return;//from  ww  w .  ja  v  a2s .  co  m
    }
    Files.walkFileTree(directory.toPath(), 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;
        }

    });
}

From source file:com.cloudbees.clickstack.util.Files2.java

/**
 * Delete given {@code dir} and its content ({@code rm -rf}).
 *
 * @param dir//from w ww .j a  va2  s  .  co m
 * @throws RuntimeIOException
 */
public static void deleteDirectory(@Nonnull Path dir) throws RuntimeIOException {
    try {
        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {

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

                logger.trace("Delete file: {} ...", file);
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {

                if (exc == null) {
                    logger.trace("Delete dir: {} ...", dir);
                    Files.delete(dir);
                    return FileVisitResult.CONTINUE;
                } else {
                    throw exc;
                }
            }

        });
    } catch (IOException e) {
        throw new RuntimeIOException("Exception deleting '" + dir + "'", e);
    }
}

From source file:com.linecorp.armeria.server.http.file.HttpFileServiceTest.java

@AfterClass
public static void destroy() throws Exception {
    server.stop();//from w ww  .  j av a  2s .co  m

    // Delete the temporary files created for testing against the real file system.
    Files.walkFileTree(tmpDir.toPath(), 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;
        }

    });
}

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   ww  w.  j a v  a  2 s. c om*/
        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: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/*from   w ww.j a  va  2  s  .c o  m*/
        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:org.egov.infra.filestore.service.impl.LocalDiskFileStoreServiceTest.java

@AfterClass
public static void afterTest() throws IOException {
    Files.deleteIfExists(tempFilePath);
    Path storePath = Paths.get(System.getProperty("user.home") + File.separator + "testfilestore");
    try {//from w  w  w  . j av  a  2  s . c o m
        Files.walkFileTree(storePath, 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 {
                if (exc == null) {
                    Files.delete(dir);
                    return FileVisitResult.CONTINUE;
                } else {
                    throw exc;
                }
            }

        });
    } catch (IOException e) {

    }
}

From source file:com.collaborne.jsonschema.generator.pojo.PojoGeneratorSmokeTest.java

@After
public void tearDown() throws IOException {
    // Dump the contents of the file system
    Path dumpStart = fs.getPath("/");
    Files.walkFileTree(dumpStart, new SimpleFileVisitor<Path>() {
        @Override/*from  w ww.  j a va  2s  .c o  m*/
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            System.out.println("> " + file);
            System.out.println(new String(Files.readAllBytes(file), StandardCharsets.UTF_8));

            // Copy the file if wanted
            if (DUMP_DIRECTORY != null) {
                Path dumpTarget = Paths.get(DUMP_DIRECTORY, name.getMethodName());
                Path target = dumpTarget.resolve(dumpStart.relativize(file).toString());
                Files.createDirectories(target.getParent());
                Files.copy(file, target, StandardCopyOption.REPLACE_EXISTING);
            }
            return FileVisitResult.CONTINUE;
        }
    });
}