Java Path File Extention nio getAllPaths(Path root, final String... extensions)

Here you can find the source of getAllPaths(Path root, final String... extensions)

Description

get All Paths

License

Apache License

Declaration

public static List<Path> getAllPaths(Path root, final String... extensions) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<Path> getAllPaths(Path root, final String... extensions) {
        List<Path> l = new ArrayList<>();
        getAllPaths(l, root, extensions);
        return l;
    }//from  ww w.j a  v  a2s  .c o  m

    public static void getAllPaths(final List<Path> paths, Path root, final String... extensions) {
        try {
            Files.walkFileTree(root.toAbsolutePath(), new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    if (Files.isRegularFile(file) && isSupportedFile(file, extensions)) {
                        paths.add(file);
                    }
                    return FileVisitResult.CONTINUE;
                }

                private boolean isSupportedFile(Path file, String[] extensions) {
                    for (String ext : extensions) {
                        if (file.toString().toLowerCase().endsWith("." + ext)) {
                            return true;
                        }
                    }
                    return false;
                }
            });
        } catch (IOException e) {
            //            return;
        }
    }
}

Related

  1. changeExtension(Path source, String extension)
  2. changeFileExtension(Path fileName, String extension)
  3. fileExtension(final Path thePath)
  4. fileRenamer(final String fromPath, final String toPath, @Nullable final String toExtension, final boolean junkPaths)
  5. findFilesWithExtension(Path directory, String targetExtension)
  6. getExtension(final Path path)
  7. getExtension(Path file)
  8. getExtension(Path path)
  9. getFileExtension(final Path file)