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

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

Description

list Files

License

Open Source License

Parameter

Parameter Description
dir directory to list
glob glob pattern for files that should be returned, which can span subdirectories as in patterns like * /*

Exception

Parameter Description
IOException if an error occurs while accessing the file system

Return

s, including both files and directories, matching the glob pattern. No path containing an element whose name starts with "." is returned. Returned paths are also ordered lexicographically.

Declaration

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

Method Source Code

//package com.java2s;
/*//  w w w . j  a  va2  s.  c o  m
 * Copyright (c) 2014, Cloudera, Inc. All Rights Reserved.
 *
 * Cloudera, Inc. licenses this file to you 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
 *
 * This software 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.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import com.google.common.base.Preconditions;

public class Main {
    /**
     * @param dir directory to list
     * @param glob glob pattern for files that should be returned, which can span subdirectories
     *  as in patterns like {@code * /*}
     * @return {@link Path}s, including both files and directories, matching the glob pattern.
     *  No path containing an element whose name starts with "." is returned.
     *  Returned paths are also ordered lexicographically.
     * @throws IOException if an error occurs while accessing the file system
     */
    public static List<Path> listFiles(Path dir, String glob) throws IOException {
        Preconditions.checkArgument(Files.isDirectory(dir), "%s is not a directory", dir);

        List<String> globLevels;
        if (glob == null || glob.isEmpty()) {
            globLevels = Collections.singletonList("*");
        } else {
            globLevels = Arrays.asList(glob.split("/"));
        }
        Preconditions.checkState(!globLevels.isEmpty());

        List<Path> paths = new ArrayList<>();
        paths.add(dir);

        for (String globLevel : globLevels) {
            List<Path> newPaths = new ArrayList<>();
            for (Path existingPath : paths) {
                if (Files.isDirectory(existingPath)) {
                    try (DirectoryStream<Path> stream = Files.newDirectoryStream(existingPath, globLevel)) {
                        for (Path path : stream) {
                            if (!path.getFileName().toString().startsWith(".")) {
                                newPaths.add(path);
                            }
                        }
                    }
                }
            }
            paths = newPaths;
        }
        Collections.sort(paths);
        return paths;
    }
}

Related

  1. listDirsRecursive(String pathStr, String pattern, int maxDepth)
  2. listFiles(final Path path)
  3. listFiles(Path base, StringBuilder b)
  4. listFiles(Path basePath)
  5. listFiles(Path dir)
  6. listFiles(Path directory)
  7. listFiles(Path directory, String glob)
  8. listFiles(Path path)
  9. listFiles(Path path)