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.yahoo.rdl.maven.RdlFileFinderImpl.java

@Override
public List<Path> findRdlFiles() throws MojoExecutionException, MojoFailureException {
    List<Path> rdlFiles = new ArrayList<Path>();
    try {//from ww w  .ja  v a2  s .  com
        Files.walkFileTree(rdlDirectory, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (FilenameUtils.isExtension(file.toString(), "rdl")) {
                    rdlFiles.add(file);
                }
                return super.visitFile(file, attrs);
            }
        });
    } catch (IOException e) {
        throw new MojoExecutionException("Error walking " + rdlDirectory.toAbsolutePath(), e);
    }
    if (rdlFiles.isEmpty()) {
        throw new MojoFailureException("No files ending in .rdl were found in the directory "
                + rdlDirectory.toAbsolutePath()
                + ". Assign <rdlDirectory> in the configuration of rdl-maven-plugin to a folder containing your rdl files.");
    }
    return rdlFiles;
}

From source file:file.FileOperatorTest.java

public static List<Path> testWalkFile() throws Exception {
    List<Path> paths = Lists.newArrayList();
    long a = System.currentTimeMillis();
    Files.walkFileTree(Paths.get(ROOT_DIR), new FileVisitor<Path>() {
        @Override/*from w  w  w.j  a  v a  2 s  .  com*/
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            paths.add(file);
            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;
        }
    });
    long b = System.currentTimeMillis();
    System.out.println(":" + (b - a));
    return paths;
}

From source file:FileMangement.PathFinder.java

@SuppressWarnings("empty-statement")
public Path[] FileCreator(String filename, boolean m3uCheck) throws IOException {

    File paths = new File(filename);
    String[] pattern;/*from   ww w  . ja v a2  s  .  c  o m*/

    if (m3uCheck != true) {
        pattern = patternCreator.patternCreator(true, false, false, false, 1);
    } else {
        pattern = new String[1];
        pattern[0] = "*.m3u";
    }

    String str = paths.toString();
    String slash = "\\";

    String s = new StringBuilder(str).append(slash).toString();
    Path startingDir = Paths.get(s);

    int i = 0;
    while (i < pattern.length) {
        Finder finder = new Finder(pattern[i]);
        Files.walkFileTree(startingDir, finder);

        listOfPaths = ArrayUtils.addAll(listOfPaths, NullCleaner(finder.returnArray()));
        finalTotal = finder.done(finalTotal);
        i++;
    }

    //    StringBuffer sb = new StringBuffer(listOfPaths[1].getFileName().toString());
    //  sb.delete(sb.length()-4,sb.length()) ;

    //  System.out.println(sb);

    //  System.out.println(Arrays.toString(listOfPaths));

    //   System.out.println("Total Matched Number of Files : " + finalTotal);

    return listOfPaths;
}

From source file:jobs.ExportDatabaseToFilesystem.java

public void doJob() throws Exception {
    Path p = PathService.getRootImageDirectory();
    Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
        @Override/*from   ww  w  .j a  v a  2 s .  c o m*/
        public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
            if (PathService.isImage(path)) {
                DatabaseImage dbi = DatabaseImage.forPath(path);
                ImportExportService.exportData(dbi);
            }
            return FileVisitResult.CONTINUE;
        }
    });
}

From source file:org.apdplat.superword.extract.PhraseExtractor.java

public static Set<String> parseDir(String dir) {
    Set<String> data = new HashSet<>();
    LOGGER.info("?" + dir);
    try {/*w  w w.  j  a  v a  2  s .  com*/
        Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                data.addAll(parseFile(file.toFile().getAbsolutePath()));
                return FileVisitResult.CONTINUE;
            }

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

From source file:com.twitter.heron.apiserver.utils.FileHelper.java

public static boolean copyDirectory(Path from, Path to) {
    try {//w  w  w . java  2  s . c om
        Files.walkFileTree(from, new CopyDirectoryVisitor(from, to));
    } catch (IOException ioe) {
        LOG.error("Failed to copy directory from {} to {}", from, to, ioe);
        return false;
    }
    return true;
}

From source file:ch.bender.evacuate.Helper.java

/**
 * Deletes a whole directory (recursively)
 * <p>//w  w  w.  j  av a  2 s  . co m
 * @param aDir
 *        a folder to be deleted (must not be null)
 * @throws IOException
 */
public static void deleteDirRecursive(Path aDir) throws IOException {
    if (aDir == null) {
        throw new IllegalArgumentException("aDir must not be null");
    }

    if (Files.notExists(aDir)) {
        return;
    }

    if (!Files.isDirectory(aDir)) {
        throw new IllegalArgumentException("given aDir is not a directory");
    }

    Files.walkFileTree(aDir, new SimpleFileVisitor<Path>() {
        /**
         * @see java.nio.file.SimpleFileVisitor#visitFileFailed(java.lang.Object, java.io.IOException)
         */
        @Override
        public FileVisitResult visitFileFailed(Path aFile, IOException aExc) throws IOException {
            if ("System Volume Information".equals((aFile.getFileName().toString()))) {
                return FileVisitResult.SKIP_SUBTREE;
            }

            throw aExc;
        }

        /**
         * @see java.nio.file.SimpleFileVisitor#preVisitDirectory(java.lang.Object, java.nio.file.attribute.BasicFileAttributes)
         */
        @Override
        public FileVisitResult preVisitDirectory(Path aFile, BasicFileAttributes aAttrs) throws IOException {
            if ("System Volume Information".equals((aFile.getFileName()))) {
                return FileVisitResult.SKIP_SUBTREE;
            }

            return FileVisitResult.CONTINUE;
        }

        @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 {
            if (dir.isAbsolute() && dir.getRoot().equals(dir)) {
                myLog.debug("root cannot be deleted: " + dir.toString());
                return FileVisitResult.CONTINUE;
            }

            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }

    });
}

From source file:io.logspace.hq.core.impl.SpacesConfiguration.java

@Bean
public Spaces createSpaces() throws IOException {
    Path path = Paths.get(this.dataDirectory, "spaces");
    this.logger.info("Using '{}' as spaces directory.", path.toAbsolutePath());

    SpaceTokensFileVisitor visitor = new SpaceTokensFileVisitor();
    Files.walkFileTree(path, visitor);

    SpacesImpl spaces = new SpacesImpl();
    spaces.setSpaceTokens(visitor.getSpaceTokens());
    return spaces;
}

From source file:org.apdplat.superword.extract.DefinitionExtractor.java

public static Set<Word> parseDir(String dir) {
    Set<Word> data = new HashSet<>();
    LOGGER.info("?" + dir);
    try {//from  w w  w  .  j a v  a  2  s  .c om
        Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                data.addAll(parseFile(file.toFile().getAbsolutePath()));
                return FileVisitResult.CONTINUE;
            }

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

From source file:org.sonar.runner.api.Utils.java

static void deleteQuietly(File f) {
    try {/* w  w w. j  ava  2  s  .  c  om*/
        Files.walkFileTree(f.toPath(), new DeleteQuietlyFileVisitor());
    } catch (IOException e) {
        // ignore
    }
}