Java Utililty Methods Path File List nio

List of utility methods to do Path File List nio

Description

The list of methods to do Path File List nio are organized into topic(s).

Method

ListlistFiles(Path directory, String glob)
 Glob matcher is explained  java.nio.file.FileSystem#getPathMatcher(String)  getPatchMatcher . 
List<Path> result = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory, glob)) {
    stream.forEach(result::add);
} catch (DirectoryIteratorException ex) {
    throw ex.getCause();
return result;
ListlistFiles(Path path)
list Files
Deque<Path> stack = new ArrayDeque<>();
final List<Path> files = new LinkedList<>();
stack.push(path);
while (!stack.isEmpty()) {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(stack.pop())) {
        for (Path entry : stream) {
            if (Files.isDirectory(entry)) {
                stack.push(entry);
...
ListlistFiles(Path path)
list Files
final List<Path> result = new ArrayList<>();
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        result.add(file);
        return FileVisitResult.CONTINUE;
});
...
voidlistFiles(Path path, List preorderList)
list Files
preorderList.add(path);
File[] listFiles = path.toFile().listFiles();
if (listFiles != null)
    for (File child : listFiles) {
        listFiles(child.toPath(), preorderList);
ListlistFiles(Path path, String glob)
list Files
final PathMatcher matcher = path.getFileSystem().getPathMatcher("glob:" + glob);
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());
};
List<Path> result = new ArrayList<Path>();
...
ListlistFilesRecursively(Path dir, final DirectoryStream.Filter filter, final boolean recursive)
list Files Recursively
final List<Path> files = new ArrayList<>();
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        if (filter.accept(file))
            files.add(file);
        return recursive ? FileVisitResult.CONTINUE : FileVisitResult.SKIP_SUBTREE;
});
return files;
ListlistFoldersInFolder2(String path)
list Folders In Folder
List<String> filesInFolder = new ArrayList<String>();
Path pathP = Paths.get(path);
try {
    DirectoryStream<Path> rootStream = Files.newDirectoryStream(pathP);
    for (Path folderName : rootStream) {
        filesInFolder.add(folderName.getFileName().toString());
} catch (IOException e) {
...
SetlistOfWords(String filePath)
Reads the file from the given path
String path = Paths.get(".").toAbsolutePath().normalize().toString();
try (Scanner scanner = new Scanner(new File(path + "/" + filePath))) {
    while (scanner.hasNext()) {
        words.add(scanner.nextLine());
    return words;
IterablelistSteps(Path scenarioDirectory)
Lists all scenario steps within the given directory path.
if (!Files.isDirectory(scenarioDirectory)) {
    throw new IllegalArgumentException(
            String.format("Path '%s' is not a directory.", scenarioDirectory.toAbsolutePath()));
File[] list = scenarioDirectory.toFile().listFiles(STEP_FILTER);
if (list == null || list.length == 0) {
    throw new IllegalStateException(
            String.format("No test steps found in directory: '%s'", scenarioDirectory.toAbsolutePath()));
...
ListloadDataFilesList(String prefix, Path bwcIndicesPath)
load Data Files List
List<String> indexes = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(bwcIndicesPath, prefix + "-*.zip")) {
    for (Path path : stream) {
        indexes.add(path.getFileName().toString());
Collections.sort(indexes);
return indexes;
...