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:io.anserini.index.IndexWebCollection.java

static Deque<Path> discoverWarcFiles(Path p, final String suffix) {

    final Deque<Path> stack = new ArrayDeque<>();

    FileVisitor<Path> fv = new SimpleFileVisitor<Path>() {

        @Override//from   w  ww.  ja v a 2  s  . c o m
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

            Path name = file.getFileName();
            if (name != null && name.toString().endsWith(suffix))
                stack.add(file);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
            if ("OtherData".equals(dir.getFileName().toString())) {
                LOG.info("Skipping: " + dir);
                return FileVisitResult.SKIP_SUBTREE;
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException ioe) {
            LOG.error("Visiting failed for " + file.toString(), ioe);
            return FileVisitResult.SKIP_SUBTREE;
        }
    };

    try {
        Files.walkFileTree(p, fv);
    } catch (IOException e) {
        LOG.error("IOException during file visiting", e);
    }
    return stack;
}

From source file:de.ncoder.studipsync.Syncer.java

public boolean isSeminarInSync(Seminar seminar) throws IOException, StudipException {
    if (!checkLevel.includes(CheckLevel.Count)) {
        return true;
    }//from ww w . ja  v  a2 s  .  c  o m

    //List downloads
    final List<Download> downloads;
    browserLock.lock();
    try {
        downloads = adapter.parseDownloads(PAGE_DOWNLOADS_LATEST, false);
    } finally {
        browserLock.unlock();
    }
    if (downloads.isEmpty()) {
        //No downloads - nothing to do
        return true;
    }

    //List local files
    final List<Path> localFiles = new LinkedList<>();
    final Path storagePath = storage.resolve(seminar);
    if (!Files.exists(storagePath)) {
        //No local files despite available downloads
        log.info(marker, "Seminar is empty!");
        return false;
    }
    Files.walkFileTree(storagePath, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            localFiles.add(file);
            return FileVisitResult.CONTINUE;
        }
    });

    //Count local files
    if (localFiles.size() < downloads.size()) {
        // Missing files
        log.warn(marker, "Seminar has only " + localFiles.size() + " local file(s) of " + downloads.size()
                + " online file(s).");
        return false;
    } else {
        // Ignore surplus files
        log.debug(marker, "Seminar has deleted file(s) left! " + localFiles.size() + " local file(s) and "
                + downloads.size() + " online file(s).");
    }

    //Check local files
    if (!areFilesInSync(downloads, localFiles)) {
        return false;
    }

    return true;
}

From source file:org.commonjava.test.compile.CompilerFixture.java

public List<File> scan(final File directory, final String pattern) throws IOException {
    final PathMatcher matcher = FileSystems.getDefault()
            .getPathMatcher("glob:" + directory.getCanonicalPath() + "/" + pattern);

    final List<File> sources = new ArrayList<>();
    Files.walkFileTree(directory.toPath(), new SimpleFileVisitor<Path>() {

        @Override//from w  ww . j  a  v a 2  s . c om
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            if (matcher.matches(file)) {
                sources.add(file.toFile());
            }

            return FileVisitResult.CONTINUE;
        }

    });

    return sources;
}

From source file:org.elasticsearch.plugins.PluginManagerIT.java

/** creates a plugin .zip and bad checksum file and returns the url for testing */
private String createPluginWithBadChecksum(final Path structure, String... properties) throws IOException {
    writeProperties(structure, properties);
    Path zip = createTempDir().resolve(structure.getFileName() + ".zip");
    try (ZipOutputStream stream = new ZipOutputStream(Files.newOutputStream(zip))) {
        Files.walkFileTree(structure, new SimpleFileVisitor<Path>() {
            @Override//  w w  w.  j  a  va  2  s .co m
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                stream.putNextEntry(new ZipEntry(structure.relativize(file).toString()));
                Files.copy(file, stream);
                return FileVisitResult.CONTINUE;
            }
        });
    }
    if (randomBoolean()) {
        writeSha1(zip, true);
    } else {
        writeMd5(zip, true);
    }
    return zip.toUri().toURL().toString();
}

From source file:org.fao.geonet.api.mapservers.GeoFile.java

/**
 * Returns the names of the raster layers (GeoTIFFs) in the geographic file.
 *
 * @return a collection of layer names//  w ww  .  j  a va2s  . c o m
 */
public Collection<String> getRasterLayers() throws IOException {
    final LinkedList<String> layers = new LinkedList<String>();
    if (zipFile != null) {
        for (Path path : zipFile.getRootDirectories()) {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    String fileName = file.getFileName().toString();
                    if (fileIsGeotif(fileName)) {
                        layers.add(getBase(fileName));
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    } else {
        String fileName = file.getFileName().toString();
        if (fileIsGeotif(fileName)) {
            layers.add(getBase(fileName));
        }
    }
    return layers;
}

From source file:org.apache.tika.batch.fs.FSBatchTestBase.java

public static void deleteDirectory(Path dir) throws IOException {
    Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
        @Override/*w  ww  .ja  va 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;
        }

    });
}

From source file:com.kegare.caveworld.util.CaveUtils.java

public static boolean archiveDirZip(final File dir, final File dest) {
    final Path dirPath = dir.toPath();
    final String parent = dir.getName();
    Map<String, String> env = Maps.newHashMap();
    env.put("create", "true");
    URI uri = dest.toURI();/*from   w  w  w  .  jav a  2 s. co m*/

    try {
        uri = new URI("jar:" + uri.getScheme(), uri.getPath(), null);
    } catch (Exception e) {
        return false;
    }

    try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
        Files.createDirectory(zipfs.getPath(parent));

        for (File file : dir.listFiles()) {
            if (file.isDirectory()) {
                Files.walkFileTree(file.toPath(), new SimpleFileVisitor<Path>() {
                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        Files.copy(file, zipfs.getPath(parent, dirPath.relativize(file).toString()),
                                StandardCopyOption.REPLACE_EXISTING);

                        return FileVisitResult.CONTINUE;
                    }

                    @Override
                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                            throws IOException {
                        Files.createDirectory(zipfs.getPath(parent, dirPath.relativize(dir).toString()));

                        return FileVisitResult.CONTINUE;
                    }
                });
            } else {
                Files.copy(file.toPath(), zipfs.getPath(parent, file.getName()),
                        StandardCopyOption.REPLACE_EXISTING);
            }
        }

        return true;
    } catch (Exception e) {
        e.printStackTrace();

        return false;
    }
}

From source file:com.google.cloud.dataflow.sdk.io.TextIOTest.java

License:asdf

@AfterClass
public static void testdownClass() throws IOException {
    Files.walkFileTree(tempFolder, new SimpleFileVisitor<Path>() {
        @Override/*from  ww w  .  j a v a2 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:de.elomagic.carafile.client.CaraCloud.java

private List<Path> getLocalChangeList(final Path basePath, final long lastScanMillis) throws IOException {
    if (Files.notExists(basePath)) {
        return Collections.EMPTY_LIST;
    }/*  w ww . j ava  2 s .  c o m*/

    final List<Path> changedPathList = new ArrayList();

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

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            if (attrs.lastModifiedTime().toMillis() > lastScanMillis) {
                changedPathList.add(dir);
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (attrs.lastModifiedTime().toMillis() > lastScanMillis) {
                changedPathList.add(file);
            }
            return FileVisitResult.CONTINUE;
        }

    });

    return changedPathList;
}

From source file:net.gcolin.simplerepo.search.SearchController.java

public void rebuild() throws IOException {
    if (running) {
        return;/*from  w  ww .ja  v  a 2  s  . c  om*/
    }
    running = true;
    configManager.setCurrentAction(REBUILD + "initialize");
    nb = 0;
    try {
        final QueryRunner run = new QueryRunner();

        try {
            Connection connection = null;
            try {
                connection = datasource.getConnection();
                connection.setAutoCommit(false);
                run.update(connection, "delete from artifacttype");
                run.update(connection, "delete from artifactversion");
                run.update(connection, "delete from artifact");
                run.update(connection, "update artifactindex set artifact=1,version=1");
                connection.commit();
            } catch (SQLException ex) {
                connection.rollback();
                throw ex;
            } finally {
                DbUtils.close(connection);
            }
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, ex);
            throw new IOException(ex);
        }
        for (final Repository repository : configManager.getConfiguration().getRepositories()) {
            File repo = new File(configManager.getRoot(), repository.getName());
            if (repo.exists()) {
                Files.walkFileTree(repo.toPath(), new SimpleFileVisitor<Path>() {
                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        nb++;
                        if (nb % 20 == 0) {
                            configManager.setCurrentAction(REBUILD + " " + nb + " files");
                        }
                        if (file.toString().endsWith(".pom")) {
                            Model model = readPom(file.toFile());
                            add(repository, file.toFile(), model);
                        }
                        return FileVisitResult.CONTINUE;
                    }

                });
            }
        }
    } finally {
        running = false;
        configManager.setCurrentAction(null);
    }
}