Java Path File List nio listFiles(Path path, String glob)

Here you can find the source of listFiles(Path path, String glob)

Description

list Files

License

Open Source License

Declaration

public static List<Path> listFiles(Path path, String glob) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<Path> listFiles(Path path, String glob) throws IOException {
        final PathMatcher matcher = path.getFileSystem().getPathMatcher("glob:" + glob);

        final DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {

            @Override/*from w  w w  .j  a  v  a2  s.c  o  m*/
            public boolean accept(Path entry) throws IOException {
                return Files.isDirectory(entry) || matcher.matches(entry.getFileName());
            }
        };
        List<Path> result = new ArrayList<Path>();

        try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, filter)) {
            for (Path entry : stream) {
                if (Files.isDirectory(entry)) {
                    result.addAll(listFiles(entry, glob));
                    return result;
                }
                result.add(entry);
            }
        }
        return result;
    }

    public static List<Path> listFiles(Path path, String glob, String exclusionGlob) throws IOException {
        final PathMatcher matcher = path.getFileSystem().getPathMatcher("glob:" + glob);

        final PathMatcher excludeMatcher = path.getFileSystem().getPathMatcher("glob:" + exclusionGlob);

        final DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {

            @Override
            public boolean accept(Path entry) throws IOException {
                return Files.isDirectory(entry)
                        || (matcher.matches(entry.getFileName()) && !excludeMatcher.matches(entry.getFileName()));
            }
        };
        List<Path> result = new ArrayList<Path>();

        try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, filter)) {
            for (Path entry : stream) {
                if (Files.isDirectory(entry)) {
                    result.addAll(listFiles(entry, glob, exclusionGlob));
                    return result;
                }
                result.add(entry);
            }
        }
        return result;
    }
}

Related

  1. listFiles(Path directory)
  2. listFiles(Path directory, String glob)
  3. listFiles(Path path)
  4. listFiles(Path path)
  5. listFiles(Path path, List preorderList)
  6. listFilesRecursively(Path dir, final DirectoryStream.Filter filter, final boolean recursive)
  7. listFoldersInFolder2(String path)
  8. listOfWords(String filePath)
  9. listSteps(Path scenarioDirectory)