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:com.sastix.cms.server.services.content.HashedDirectoryServiceTest.java

private void deleteRecursive(Path path) throws IOException {
    Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
        @Override/*from ww w  . ja va2s. 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: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;
    }//from   ww w . ja va  2s. c  om

    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: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 www.  j  a v  a 2  s.c  o  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:org.openeos.tools.maven.plugins.GenerateEntitiesMojo.java

private Set<File> checkForLocalFiles(File file) throws IOException, URISyntaxException {
    final Set<File> result = new TreeSet<File>();
    final PathMatcher matcher = FileSystems.getDefault()
            .getPathMatcher("glob:" + file.getAbsolutePath() + "/" + searchpath);
    SimpleFileVisitor<Path> fileVisitor = new SimpleFileVisitor<Path>() {

        @Override/* w w w . j  a va 2 s . co m*/
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (matcher.matches(file)) {
                result.add(file.toFile());
            }
            return FileVisitResult.CONTINUE;
        }

    };
    Files.walkFileTree(file.toPath(), fileVisitor);
    return result;
}

From source file:com.yahoo.parsec.gradle.utils.FileUtils.java

/**
 * Find files in path./*w  ww  .j  a  va  2s.co  m*/
 *
 * @param path    find path
 * @param pattern find patttern
 * @return a set of path
 * @throws IOException IOException
 */
public Set<Path> findFiles(String path, String pattern) throws IOException {
    try {
        Set<Path> paths = new HashSet<>();
        PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(pattern);

        Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path filePath, BasicFileAttributes attrs) {
                if (pathMatcher.matches(filePath.getFileName())) {
                    paths.add(filePath);
                }
                return FileVisitResult.CONTINUE;
            }
        });

        return paths;
    } catch (IOException e) {
        throw e;
    }
}

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

public void rebuild() throws IOException {
    if (running) {
        return;/*from  ww w.  j a v a2 s .c  o m*/
    }
    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);
    }
}

From source file:science.atlarge.graphalytics.powergraph.PowergraphPlatform.java

@Override
public BenchmarkMetrics finalize(RunSpecification runSpecification) {
    stopPlatformLogging();//  ww w  .  j a  va 2s  .c  om
    BenchmarkRunSetup benchmarkRunSetup = runSpecification.getBenchmarkRunSetup();
    BenchmarkRun benchmarkRun = runSpecification.getBenchmarkRun();

    Path platformLogPath = benchmarkRunSetup.getLogDir().resolve("platform");

    final List<Double> superstepTimes = new ArrayList<>();

    try {
        Files.walkFileTree(platformLogPath, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String logs = FileUtil.readFile(file);
                for (String line : logs.split("\n")) {
                    if (line.contains("- run algorithm:")) {
                        Pattern regex = Pattern.compile(".* - run algorithm: ([+-]?([0-9]*[.])?[0-9]+) sec.*");
                        Matcher matcher = regex.matcher(line);
                        matcher.find();
                        superstepTimes.add(Double.parseDouble(matcher.group(1)));
                    }
                }
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (superstepTimes.size() != 0) {
        Double procTime = 0.0;
        for (Double superstepTime : superstepTimes) {
            procTime += superstepTime;
        }

        BenchmarkMetrics metrics = new BenchmarkMetrics();
        BigDecimal procTimeS = (new BigDecimal(procTime)).setScale(3, RoundingMode.CEILING);
        metrics.setProcessingTime(new BenchmarkMetric(procTimeS, "s"));

        return metrics;
    } else {
        LOG.error("Failed to find any metrics regarding superstep runtime.");
        return new BenchmarkMetrics();
    }
}

From source file:org.roda.core.storage.fs.FSUtils.java

/**
 * Copies a directory/file from one path to another
 * //from   ww  w . j  a v a  2 s  . com
 * @param sourcePath
 *          source path
 * @param targetPath
 *          target path
 * @param replaceExisting
 *          true if the target directory/file should be replaced if it already
 *          exists; false otherwise
 * @throws AlreadyExistsException
 * @throws GenericException
 */
public static void copy(final Path sourcePath, final Path targetPath, boolean replaceExisting)
        throws AlreadyExistsException, GenericException {

    // check if we can replace existing
    if (!replaceExisting && FSUtils.exists(targetPath)) {
        throw new AlreadyExistsException("Cannot copy because target path already exists: " + targetPath);
    }

    // ensure parent directory exists or can be created
    try {
        if (targetPath != null) {
            Files.createDirectories(targetPath.getParent());
        }
    } catch (IOException e) {
        throw new GenericException("Error while creating target directory parent folder", e);
    }

    if (FSUtils.isDirectory(sourcePath)) {
        try {
            Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs)
                        throws IOException {
                    Files.createDirectories(targetPath.resolve(sourcePath.relativize(dir)));
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs)
                        throws IOException {
                    Files.copy(file, targetPath.resolve(sourcePath.relativize(file)));
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            throw new GenericException("Error while copying one directory into another", e);
        }
    } else {
        try {

            CopyOption[] copyOptions = replaceExisting
                    ? new CopyOption[] { StandardCopyOption.REPLACE_EXISTING }
                    : new CopyOption[] {};
            Files.copy(sourcePath, targetPath, copyOptions);
        } catch (IOException e) {
            throw new GenericException("Error while copying one file into another", e);
        }

    }

}

From source file:gov.noaa.pfel.coastwatch.util.FileVisitorDNLS.java

/** Invoked for a file in a directory. */
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

    int oSize = directoryPA.size();
    try {/*from  www. jav a2  s. c  o  m*/
        String name = file.getFileName().toString();
        if (!fileNamePattern.matcher(name).matches()) {
            if (debugMode)
                String2.log(">> fileName doesn't match: name=" + name + " regex=" + fileNameRegex);
            return FileVisitResult.CONTINUE;
        }

        //getParent returns \\ or /, without trailing /
        String ttDir = String2.replaceAll(file.getParent().toString(), fromSlash, toSlash) + toSlash;
        if (debugMode)
            String2.log(">> add fileName: " + ttDir + name);
        directoryPA.add(ttDir);
        namePA.add(name);
        lastModifiedPA.add(attrs.lastModifiedTime().toMillis());
        sizePA.add(attrs.size());
        //for debugging only:
        //String2.log(ttDir + name + 
        //    " mod=" + attrs.lastModifiedTime().toMillis() +
        //    " size=" + attrs.size());
    } catch (Throwable t) {
        if (directoryPA.size() > oSize)
            directoryPA.remove(oSize);
        if (namePA.size() > oSize)
            namePA.remove(oSize);
        if (lastModifiedPA.size() > oSize)
            lastModifiedPA.remove(oSize);
        if (sizePA.size() > oSize)
            sizePA.remove(oSize);
        String2.log(MustBe.throwableToString(t));
    }

    return FileVisitResult.CONTINUE;
}

From source file:com.liferay.sync.engine.SyncSystemTest.java

protected void addFolder(Path testFilePath, JsonNode stepJsonNode) throws Exception {

    SyncSite syncSite = getSyncSite(stepJsonNode);

    String dependency = getString(stepJsonNode, "dependency");

    final Path dependencyFilePath = getDependencyFilePath(testFilePath, dependency);

    FileSystem fileSystem = FileSystems.getDefault();

    final Path targetFilePath = Paths.get(FileUtil.getFilePathName(syncSite.getFilePathName(),
            dependency.replace("common" + fileSystem.getSeparator(), "")));

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

        @Override/* w  w w .  j  a v a 2  s  . c  om*/
        public FileVisitResult preVisitDirectory(Path filePath, BasicFileAttributes basicFileAttributes)
                throws IOException {

            Path relativeFilePath = dependencyFilePath.relativize(filePath);

            Files.createDirectories(targetFilePath.resolve(relativeFilePath));

            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path filePath, BasicFileAttributes basicFileAttributes)
                throws IOException {

            Path relativeFilePath = dependencyFilePath.relativize(filePath);

            Files.copy(filePath, targetFilePath.resolve(relativeFilePath));

            return FileVisitResult.CONTINUE;
        }

    });
}