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:de.fatalix.bookery.bl.background.importer.CalibriImporter.java

private void processArchive(final Path zipFile, final int batchSize) throws IOException {
    try (FileSystem zipFileSystem = FileSystems.newFileSystem(zipFile, null)) {
        final List<BookEntry> bookEntries = new ArrayList<>();
        Path root = zipFileSystem.getPath("/");
        Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

            @Override/*from   w  w  w.jav a2s  .  co  m*/
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                if (dir.toString().contains("__MACOSX")) {
                    return FileVisitResult.SKIP_SUBTREE;
                }
                try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dir)) {
                    BookEntry bookEntry = new BookEntry().setUploader("admin");
                    for (Path path : directoryStream) {
                        if (!Files.isDirectory(path)) {
                            if (path.toString().contains(".opf")) {
                                bookEntry = parseOPF(path, bookEntry);
                            }
                            if (path.toString().contains(".mobi")) {
                                bookEntry.setMobi(Files.readAllBytes(path)).setMimeType("MOBI");
                            }
                            if (path.toString().contains(".epub")) {
                                bookEntry.setEpub(Files.readAllBytes(path));
                            }
                            if (path.toString().contains(".jpg")) {
                                bookEntry.setCover(Files.readAllBytes(path));
                                ByteArrayOutputStream output = new ByteArrayOutputStream();
                                Thumbnails.of(new ByteArrayInputStream(bookEntry.getCover())).size(130, 200)
                                        .toOutputStream(output);
                                bookEntry.setThumbnail(output.toByteArray());
                                bookEntry.setThumbnailGenerated("done");
                            }
                        }
                    }
                    if (bookEntry.getMobi() != null || bookEntry.getEpub() != null) {
                        bookEntries.add(bookEntry);
                        if (bookEntries.size() > batchSize) {
                            logger.info("Adding " + bookEntries.size() + " Books...");
                            try {
                                solrHandler.addBeans(bookEntries);
                            } catch (SolrServerException ex) {
                                logger.error(ex, ex);
                            }
                            bookEntries.clear();
                        }
                    }
                } catch (IOException ex) {
                    logger.error(ex, ex);
                }
                return super.preVisitDirectory(dir, attrs);
            }
        });
        try {
            if (!bookEntries.isEmpty()) {
                logger.info("Adding " + bookEntries.size() + " Books...");
                solrHandler.addBeans(bookEntries);
            }
        } catch (SolrServerException ex) {
            logger.error(ex, ex);
        }
    } finally {
        Files.delete(zipFile);
    }
}

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

public static void deleteRecursive(Path f) {
    try {//from  w  w  w.  ja  v  a 2 s  .c  o 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.apdplat.superword.extract.HyphenExtractor.java

public static Map<String, AtomicInteger> parseZip(String zipFile) {
    Map<String, AtomicInteger> data = new HashMap<>();
    LOGGER.info("?ZIP" + zipFile);
    try (FileSystem fs = FileSystems.newFileSystem(Paths.get(zipFile), WordClassifier.class.getClassLoader())) {
        for (Path path : fs.getRootDirectories()) {
            LOGGER.info("?" + path);
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

                @Override/* w w  w .j a  v  a  2s . c  o  m*/
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    LOGGER.info("?" + file);
                    // ?
                    Path temp = Paths.get("target/origin-html-temp.txt");
                    Files.copy(file, temp, StandardCopyOption.REPLACE_EXISTING);
                    Map<String, AtomicInteger> rs = parseFile(temp.toFile().getAbsolutePath());
                    rs.keySet().forEach(k -> {
                        data.putIfAbsent(k, new AtomicInteger());
                        data.get(k).addAndGet(rs.get(k).get());
                    });
                    return FileVisitResult.CONTINUE;
                }

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

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 .j av a 2  s  . c o  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);
        }
    }
}

From source file:io.mangoo.build.Watcher.java

@SuppressWarnings("all")
private void registerAll(final Path path) throws IOException {
    Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
        @Override/*from  w  w  w .  j  a  va2s .  c  om*/
        public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attrs) throws IOException {
            register(path);
            return FileVisitResult.CONTINUE;
        }
    });
}

From source file:org.hawkular.apm.api.services.ConfigurationLoader.java

/**
 * This method loads the configuration from the supplied URI.
 *
 * @param uri The URI/*from  w ww.ja va2  s. co m*/
 * @param type The type, or null if default (jvm)
 * @return The configuration
 */
protected static CollectorConfiguration loadConfig(String uri, String type) {
    final CollectorConfiguration config = new CollectorConfiguration();

    if (type == null) {
        type = DEFAULT_TYPE;
    }

    uri += java.io.File.separator + type;

    File f = new File(uri);

    if (!f.isAbsolute()) {
        if (f.exists()) {
            uri = f.getAbsolutePath();
        } else if (System.getProperties().containsKey("jboss.server.config.dir")) {
            uri = System.getProperty("jboss.server.config.dir") + java.io.File.separatorChar + uri;
        } else {
            try {
                URL url = Thread.currentThread().getContextClassLoader().getResource(uri);
                if (url != null) {
                    uri = url.getPath();
                } else {
                    log.severe("Failed to get absolute path for uri '" + uri + "'");
                }
            } catch (Exception e) {
                log.log(Level.SEVERE, "Failed to get absolute path for uri '" + uri + "'", e);
                uri = null;
            }
        }
    }

    if (uri != null) {
        String[] uriParts = uri.split(Matcher.quoteReplacement(File.separator));
        int startIndex = 0;

        // Remove any file prefix
        if (uriParts[0].equals("file:")) {
            startIndex++;
        }

        try {
            Path path = getPath(startIndex, uriParts);

            Files.walkFileTree(path, new FileVisitor<Path>() {

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

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

                @Override
                public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                    if (path.toString().endsWith(".json")) {
                        String json = new String(Files.readAllBytes(path));
                        CollectorConfiguration childConfig = mapper.readValue(json,
                                CollectorConfiguration.class);
                        if (childConfig != null) {
                            config.merge(childConfig, false);
                        }
                    }
                    return FileVisitResult.CONTINUE;
                }

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

            });
        } catch (Throwable e) {
            log.log(Level.SEVERE, "Failed to load configuration", e);
        }
    }

    return config;
}

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   www .j a  va 2  s .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:org.apache.beam.sdk.extensions.sql.impl.schema.text.BeamTextCSVTableTest.java

@AfterClass
public static void teardownClass() throws IOException {
    Files.walkFileTree(tempFolder, new SimpleFileVisitor<Path>() {

        @Override//from   www.  j av  a  2 s  .c om
        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.neo4j.browser.CannedCypherExecutionTest.java

@Test
public void shouldBeAbleToExecuteAllTheCannedCypherQueriesContainedInStaticHtmlFiles() throws Exception {
    URL resourceLoc = getClass().getClassLoader().getResource("browser");
    assertNotNull(resourceLoc);// w  w  w. j  a  v  a  2s. c om

    final AtomicInteger explainCount = new AtomicInteger(0);
    final AtomicInteger executionCount = new AtomicInteger(0);

    Files.walkFileTree(Paths.get(resourceLoc.toURI()), new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
            final GraphDatabaseService database = new TestGraphDatabaseFactory().newImpermanentDatabase();

            String fileName = file.getFileName().toString();
            if (fileName.endsWith(".html")) {
                String content = FileUtils.readTextFile(file.toFile(), Charsets.UTF_8);
                Elements cypherElements = Jsoup.parse(content).select("pre.runnable")
                        .not(".standalone-example");
                for (Element cypherElement : cypherElements) {
                    String statement = replaceAngularExpressions(cypherElement.text());

                    if (!statement.startsWith(":")) {
                        if (shouldExplain(statement)) {
                            try (Transaction transaction = database.beginTx()) {
                                Iterable<Notification> actual = database.execute(prependExplain(statement))
                                        .getNotifications();
                                boolean skipKnownInefficientCypher = !cypherElement.parent().select(".warn")
                                        .isEmpty();
                                if (skipKnownInefficientCypher) {

                                    List<Notification> targetCollection = new ArrayList<Notification>();
                                    CollectionUtils.addAll(targetCollection, actual);
                                    CollectionUtils.filter(targetCollection,
                                            new org.apache.commons.collections4.Predicate<Notification>()

                                            {
                                                @Override
                                                public boolean evaluate(Notification notification) {
                                                    return notification.getDescription()
                                                            .contains(NotificationCode.CARTESIAN_PRODUCT
                                                                    .values().toString());
                                                }
                                            });

                                    assertThat(
                                            format("Query [%s] should only produce cartesian product "
                                                    + "notifications. [%s]", statement, fileName),
                                            targetCollection, empty());

                                    explainCount.incrementAndGet();
                                    transaction.success();

                                } else {
                                    assertThat(format("Query [%s] should produce no notifications. [%s]",
                                            statement, fileName), actual, is(emptyIterable()));
                                    explainCount.incrementAndGet();
                                    transaction.success();
                                }
                            } catch (QueryExecutionException e) {
                                throw new AssertionError(
                                        format("Failed to explain query [%s] in file [%s]", statement, file),
                                        e);
                            }
                        }
                        try (Transaction transaction = database.beginTx()) {
                            database.execute(statement);
                            executionCount.incrementAndGet();
                            transaction.success();
                        } catch (QueryExecutionException e) {
                            throw new AssertionError(
                                    format("Failed to execute query [%s] in file [%s]", statement, file), e);
                        }
                    }
                }
            }
            return FileVisitResult.CONTINUE;
        }
    });

    assertTrue("Static files should contain at least one valid cypher statement",
            executionCount.intValue() >= 1);
    System.out.printf("Explained %s cypher statements extracted from HTML files, with no notifications.%n",
            explainCount);
    System.out.printf("Executed %s cypher statements extracted from HTML files, with no errors.%n",
            executionCount);
}

From source file:org.dragoneronca.util.Configuration.java

/**
 * Init logger and configuration properties.
 * <p/>/*from w  w w.  j  a  v  a2 s  .c o m*/
 * The configuration directory should contain at least: <ul> <li>file
 * &quot;environment.properties&quot;</li> <li>file &quot;log4j.properties&quot;</li> <li>file
 * &quot;log4j-debug.properties&quot;</li> </ul>
 *
 * @param confDir path to the configuration directory
 */
public Configuration(Path confDir) {

    String productLogConfig = confDir.toAbsolutePath().toString() + File.separator + PRODUCT_LOG_CONFIG;
    String developLogConfig = confDir.toAbsolutePath().toString() + File.separator + DEVELOP_LOG_CONFIG;

    // default configuration for log4j
    LogManager.resetConfiguration();
    if (Files.exists(Paths.get(productLogConfig))) {
        PropertyConfigurator.configure(productLogConfig);
    } else {
        BasicConfigurator.configure();
        Logger.getRootLogger().setLevel(Level.INFO);
        LOG.warn("Impossible to load configuration for log4j: " + productLogConfig);
        LOG.warn("Using basic configuration for log4j");
    }

    try {
        Files.walkFileTree(confDir, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Matcher confMatcher = CONF_PATTERN.matcher(file.getFileName().toString());
                if (confMatcher.matches()) {

                    LOG.info("Loading configuration file: " + file.toAbsolutePath().toString());

                    try {
                        configurationMap.put(confMatcher.group("name"),
                                new PropertiesConfiguration(file.toAbsolutePath().toString()));
                    } catch (ConfigurationException e) {
                        LOG.warn("Exception while loading: " + file.getFileName().toString());
                    }
                }
                return FileVisitResult.CONTINUE;
            }

        });
    } catch (IOException e) {
        LOG.error("Impossible to load configuration files", e);
        System.exit(-1);
    }

    PropertiesConfiguration envConf = configurationMap.get(ENV_CONF);
    if (envConf != null) {
        String envVar = envConf.getString(ENV_VAR);
        if (envVar != null) {
            Environment tmpEnv = Environment.getByName(envVar);
            if (tmpEnv != null) {
                environment = tmpEnv;
            } else {
                environment = Environment.PRODUCTION;
                LOG.warn("Invalid value for the environment variable in configuration file: "
                        + "config/environment.properties "
                        + "- Only \"development\" and \"production\" are allowed");
            }
        } else {
            environment = Environment.PRODUCTION;
            LOG.warn("Missing environment variable in configuration file: " + "config/environment.properties");
        }
    } else {
        environment = Environment.PRODUCTION;
        LOG.warn("Missing environment configuration file, create: " + "config/environment.properties");
    }

    // advanced logger configuration
    if (environment.equals(Environment.DEVELOPMENT)) {
        LogManager.resetConfiguration();
        if (Files.exists(Paths.get(developLogConfig))) {
            PropertyConfigurator.configure(developLogConfig);
        } else {
            LOG.warn("Impossible to load development configuration for log4j: " + developLogConfig);
            LOG.warn("Using the previous configuration for log4j");
        }
    }
}