Java Path File List nio listChildren(Path directory)

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

Description

Traverse a directory an return children files with depth 0.

License

Open Source License

Parameter

Parameter Description
directory input Path, should be a directory

Exception

Parameter Description
IllegalArgumentException if input Path is not a directory
IOException if an error occurs during directory scanning

Return

children Path

Declaration

public static List<Path> listChildren(Path directory) throws IllegalArgumentException, IOException 

Method Source Code


//package com.java2s;
/*/*from w w  w  .  ja va 2s .  com*/
 *    Geotoolkit.org - An Open Source Java GIS Toolkit
 *    http://www.geotoolkit.org
 *
 *    (C) 2008-2012, Open Source Geospatial Foundation (OSGeo)
 *    (C) 2009-2012, Geomatys
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */

import java.io.*;

import java.nio.file.*;

import java.util.*;

public class Main {
    /**
     * Traverse a directory an return children files with depth 0.
     * Result of this method can result of an OutOfMemory if scanned
     * folder contains very large number of files.
     *
     * @param directory input Path, should be a directory
     * @return children Path
     * @throws IllegalArgumentException if input Path is not a directory
     * @throws IOException if an error occurs during directory scanning
     */
    public static List<Path> listChildren(Path directory) throws IllegalArgumentException, IOException {
        return listChildren(directory, "*");
    }

    /**
     * Traverse a directory an return children files with depth 0.
     * Result of this method can result of an OutOfMemory if scanned
     * folder contains very large number of files.
     *
     * @param directory input Path, should be a directory
     * @param glob glob filter
     * @return children Path
     * @throws IllegalArgumentException if input Path is not a directory
     * @throws IOException if an error occurs during directory scanning
     */
    public static List<Path> listChildren(Path directory, String glob)
            throws IllegalArgumentException, IOException {
        if (!Files.isDirectory(directory)) {
            throw new IllegalArgumentException("Input Path is not a directory or doesn't exist");
        }

        if (glob == null || glob.isEmpty()) {
            glob = "*";
        }

        final List<Path> children = new LinkedList<>();
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory, glob)) {
            for (Path child : stream) {
                children.add(child);
            }
        }
        //asc sort
        Collections.sort(children);
        return children;
    }
}

Related

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