Java Path File List nio listChildren(Path directory)

Here you can find the source of listChildren(Path directory)

Description

list Children

License

Apache License

Declaration

public static Set<Path> listChildren(Path directory) 

Method Source Code


//package com.java2s;
/*//  w ww .jav  a2 s .  c o m
 * Copyright (C) 2016 Thiago Gutenberg Carvalho da Costa
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;

import java.util.Collections;
import java.util.HashSet;

import java.util.Set;

public class Main {
    public static final String DEFAULT_GLOB = "*";

    public static Set<Path> listChildren(Path directory) {
        return listChildren(directory, DEFAULT_GLOB);
    }

    /**
     * List children path from the provided directory and the glob filter.
     *
     * @param directory the directory
     * @param glob      the glob filter
     * @return a set of found path children
     */
    public static Set<Path> listChildren(Path directory, String glob) {
        if (directory == null || !Files.isDirectory(directory)) {
            return Collections.emptySet();
        }
        glob = glob == null || glob.trim().isEmpty() ? DEFAULT_GLOB : glob;
        Set<Path> children = new HashSet<>();
        try (DirectoryStream<Path> childrenStream = Files.newDirectoryStream(directory, glob)) {
            childrenStream.forEach(children::add);
            return Collections.unmodifiableSet(new HashSet<>(children));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. isPlaylistFile(Path testFile)
  2. list(final Path directory)
  3. list(Path basePath)
  4. listChildren(Path dir)
  5. listChildren(Path dir, boolean searchDirectory)
  6. listChildren(Path directory)
  7. listDir(Path dir)
  8. listDirsRecursive(String pathStr, String pattern, int maxDepth)
  9. listFiles(final Path path)