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:de.fatalix.book.importer.CalibriImporter.java

public static void processBooks(Path root, String solrURL, String solrCore, final int batchSize)
        throws IOException, SolrServerException {
    final SolrServer solrServer = SolrHandler.createConnection(solrURL, solrCore);
    final List<BookEntry> bookEntries = new ArrayList<>();
    Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

        @Override/* w  ww . j a  v a2s.c  o  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) {
                        System.out.println("Adding " + bookEntries.size() + " Books...");
                        try {
                            SolrHandler.addBeans(solrServer, bookEntries);
                        } catch (SolrServerException ex) {
                            System.out.println(ex.getMessage());
                            ex.printStackTrace();
                        }
                        bookEntries.clear();
                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return super.preVisitDirectory(dir, attrs);
        }
    });
}

From source file:org.bonitasoft.web.designer.migration.LiveMigration.java

public void start() throws IOException {

    repository.walk(new SimpleFileVisitor<Path>() {

        @Override/*from  w  w  w  .j ava  2  s .  c om*/
        public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
            migrate(repository, path.toFile().toPath());
            return CONTINUE;
        }
    });

    repository.watch(path -> migrate(repository, path));
}

From source file:org.apdplat.superword.tools.JavaCodeAnalyzer.java

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

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

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

From source file:org.neo4j.io.fs.FileUtils.java

public static void deletePathRecursively(Path path) throws IOException {
    Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
        @Override//from  www. ja v a2 s  .  com
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            deleteFileWithRetries(file, 0);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
            if (e != null) {
                throw e;
            }
            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }
    });
}

From source file:desktopsearch.WatchDir.java

/**
 * Register the given directory, and all its sub-directories, with the
 * WatchService.//from   w w  w .ja va 2s  .c  o  m
 */
private void registerAll(final Path start) {
    // register directory and sub-directories
    try {
        Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                register(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:fi.hsl.parkandride.ExportQTypes.java

private static void deleteOldQTypes(Path packageDir) throws IOException {
    Files.walkFileTree(packageDir, new SimpleFileVisitor<Path>() {
        @Override/* w  ww.j a  v a2 s.c  o  m*/
        public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
            if (path.getFileName().toString().startsWith(NAME_PREFIX)) {
                Files.delete(path);
            }
            return FileVisitResult.CONTINUE;
        }
    });
}

From source file:io.crate.testing.Utils.java

public static void deletePath(Path path) throws IOException {
    Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
        @Override//from w  w w .j  a v  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.apdplat.superword.extract.HyphenExtractor.java

public static Map<String, AtomicInteger> parseDir(String dir) {
    Map<String, AtomicInteger> data = new HashMap<>();
    LOGGER.info("?" + dir);
    try {/*  w  w  w  .  j a  va 2s. c o  m*/
        Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Map<String, AtomicInteger> rs = parseFile(file.toFile().getAbsolutePath());
                rs.keySet().forEach(k -> {
                    data.putIfAbsent(k, new AtomicInteger());
                    data.get(k).addAndGet(rs.get(k).get());
                });
                return FileVisitResult.CONTINUE;
            }

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

From source file:nl.salp.warcraft4j.config.PropertyWarcraft4jConfigTest.java

private static void delete(Path directory) throws Exception {
    Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
        @Override//from  ww w .  ja v a 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 e) throws IOException {
            if (e == null) {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            } else {
                throw e;
            }
        }
    });
}

From source file:services.ImportExportService.java

public static List<DatabaseImage> findImportables(Path path) {
    final List<DatabaseImage> importable = new LinkedList<DatabaseImage>();
    try {/*from w w  w.ja va 2s  .  c  om*/
        Files.walkFileTree(path, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
                new SimpleFileVisitor<Path>() {
                    @Override
                    public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                        if (PathService.isImage(path)) {
                            DatabaseImage image = DatabaseImage.forPath(path);
                            if (hasFileToImport(image) && !image.imported) {
                                importable.add(image);
                            }
                        }
                        return FileVisitResult.CONTINUE;
                    }
                });
    } catch (IOException io) {
        io.printStackTrace();
        return importable;
    }
    return importable;
}