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:com.upplication.s3fs.util.AmazonS3ClientMock.java

public void clear() {
    try {//from  w ww  . j av  a2s  .co m
        Files.walkFileTree(base, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                if (dir != base)
                    Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.mycore.common.xml.MCRXMLFunctions.java

/**
 * Method returns the amount of space consumed by the files contained in the
 * derivate container. The returned string is already formatted meaning it
 * has already the optimal measurement unit attached (e.g. 142 MB, ).
 *
 * @param derivateId/* ww  w .j  a  v  a 2s. co  m*/
 *            the derivate id for which the size should be returned
 * @return the size as formatted string
 */
public static String getSize(String derivateId) throws IOException {
    MCRPath rootPath = MCRPath.getPath(derivateId, "/");
    final AtomicLong size = new AtomicLong();
    Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            size.addAndGet(attrs.size());
            return super.visitFile(file, attrs);
        }

    });
    return MCRUtils.getSizeFormatted(size.get());
}

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

/**
 * Update file and dir permissions./* w w  w .  jav a 2  s.c  om*/
 *
 * @param path
 * @param filePermissions
 * @param dirPermissions
 * @throws RuntimeIOException
 */
private static void chmodOverwritePermissions(@Nonnull Path path,
        @Nonnull final Set<PosixFilePermission> filePermissions,
        @Nonnull final Set<PosixFilePermission> dirPermissions) throws RuntimeIOException {
    if (!Files.exists(path)) {
        throw new IllegalArgumentException("Given path " + path + " does not exist");
    }

    SimpleFileVisitor<Path> setReadOnlyFileVisitor = new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (Files.isDirectory(file)) {
                throw new IllegalStateException("No dir expected here: " + file);
            } else {
                Files.setPosixFilePermissions(file, filePermissions);
            }
            return super.visitFile(file, attrs);
        }

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            Files.setPosixFilePermissions(dir, dirPermissions);
            return super.preVisitDirectory(dir, attrs);
        }
    };
    try {
        Files.walkFileTree(path, setReadOnlyFileVisitor);
    } catch (IOException e) {
        throw new RuntimeIOException("Exception setting permissions file permissions to " + filePermissions
                + " and folder permissions to " + dirPermissions + " on " + path, e);
    }
}

From source file:net.sourceforge.pmd.docs.RuleDocGenerator.java

/**
 * Searches for the source file of the given ruleset. This provides the information
 * for the "editme" link.//from   ww w  . ja v  a2 s  .  co m
 *
 * @param ruleset the ruleset to search for.
 * @return
 * @throws IOException
 */
private String getRuleSetSourceFilepath(RuleSet ruleset) throws IOException {
    final String rulesetFilename = FilenameUtils.normalize(StringUtils.chomp(ruleset.getFileName()));
    final List<Path> foundPathResult = new LinkedList<>();

    Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            String path = file.toString();
            if (path.contains("src") && path.endsWith(rulesetFilename)) {
                foundPathResult.add(file);
                return FileVisitResult.TERMINATE;
            }
            return super.visitFile(file, attrs);
        }
    });

    if (!foundPathResult.isEmpty()) {
        Path foundPath = foundPathResult.get(0);
        foundPath = root.relativize(foundPath);
        // Note: the path is normalized to unix path separators, so that the editme link
        // uses forward slashes
        return FilenameUtils.normalize(foundPath.toString(), true);
    }

    return StringUtils.chomp(ruleset.getFileName());
}

From source file:org.olat.ims.qti.qpool.QTIImportProcessor.java

private List<DocInfos> traverseZip_nio(File file) throws IOException {
    List<DocInfos> docInfos = new ArrayList<>();

    Path fPath = FileSystems.newFileSystem(file.toPath(), null).getPath("/");
    if (fPath != null) {
        DocInfosVisitor visitor = new DocInfosVisitor();
        Files.walkFileTree(fPath, visitor);

        List<Path> xmlFiles = visitor.getXmlFiles();
        for (Path xmlFile : xmlFiles) {
            InputStream in = Files.newInputStream(xmlFile);

            Document doc = readXml(in);
            if (doc != null) {
                DocInfos d = new DocInfos();
                d.setDocument(doc);//from   www . ja  v a 2  s.  c  om
                d.setRoot(xmlFile.getParent());
                d.setFilename(xmlFile.getFileName().toString());
                docInfos.add(d);
            }

        }
    }

    return docInfos;
}

From source file:org.schedulesdirect.grabber.Grabber.java

private void removeExpiredSchedules(FileSystem vfs) throws IOException, JSONException {
    final int[] i = new int[] { 0 };
    final Path root = vfs.getPath("schedules");
    Files.walkFileTree(root, new FileVisitor<Path>() {

        @Override// w  w  w .j a  v a2 s.c om
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            return !Files.isSameFile(dir, root) ? FileVisitResult.SKIP_SUBTREE : FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            try (InputStream ins = Files.newInputStream(file)) {
                JSONArray sched = Config.get().getObjectMapper()
                        .readValue(IOUtils.toString(ins, ZipEpgClient.ZIP_CHARSET.toString()), JSONObject.class)
                        .getJSONArray("programs");
                if (isScheduleExpired(sched)) {
                    Files.delete(file);
                    ++i[0];
                }
            }
            return FileVisitResult.CONTINUE;
        }

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

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

    });
    LOG.info(String.format("Removed %d expired schedule(s).", i[0]));
}

From source file:org.eclipse.cdt.arduino.core.internal.board.ArduinoManager.java

public static void recursiveDelete(Path directory) throws IOException {
    Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
        @Override/*from   w  ww. j  ava2s.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:net.sourceforge.pmd.docs.RuleDocGenerator.java

private String getRuleClassSourceFilepath(String ruleClass) throws IOException {
    final String relativeSourceFilename = ruleClass.replaceAll("\\.", Matcher.quoteReplacement(File.separator))
            + ".java";
    final List<Path> foundPathResult = new LinkedList<>();

    Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
        @Override//from  w  w  w.j  a va  2 s.  c  o  m
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            String path = file.toString();
            if (path.contains("src") && path.endsWith(relativeSourceFilename)) {
                foundPathResult.add(file);
                return FileVisitResult.TERMINATE;
            }
            return super.visitFile(file, attrs);
        }
    });

    if (!foundPathResult.isEmpty()) {
        Path foundPath = foundPathResult.get(0);
        foundPath = root.relativize(foundPath);
        return FilenameUtils.normalize(foundPath.toString(), true);
    }

    return FilenameUtils.normalize(relativeSourceFilename, true);
}

From source file:org.kurento.test.services.KmsService.java

private void deleteFolderAndContent(Path folder) throws IOException {
    if (folder != null) {
        Files.walkFileTree(folder, new SimpleFileVisitor<Path>() {
            @Override//from   w  w  w. j  a v a2s. co  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;
            }
        });
    }
}