Java Path File List nio listChildren(Path dir)

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

Description

Gets children paths from the give directory.

License

Open Source License

Parameter

Parameter Description
dir path to the directory.

Exception

Parameter Description
IOException if something goes wrong.

Return

the children paths from the give directory.

Declaration

public static List<Path> listChildren(Path dir) throws IOException 

Method Source Code


//package com.java2s;
import java.io.IOException;

import java.nio.file.*;

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

public class Main {
    /**//from  w ww  .  j  a  v  a  2s.c  om
     * Gets children paths from the give directory.
     *
     * @param dir path to the directory.
     * @return the children paths from the give directory.
     * @throws IOException if something goes wrong.
     */
    public static List<Path> listChildren(Path dir) throws IOException {
        return listChildren(dir, null);
    }

    /**
     * Gets children paths from the give directory appling the given filter.
     *
     * @param dir    path to the directory.
     * @param filter the filter to be applied
     * @return the children paths from the give directory.
     * @throws IOException if something goes wrong.
     */
    public static List<Path> listChildren(Path dir, DirectoryStream.Filter<? super Path> filter)
            throws IOException {
        if (isFolder(dir)) {
            List<Path> list = new ArrayList<>();
            try (DirectoryStream<Path> directoryStream = (filter != null ? Files.newDirectoryStream(dir, filter)
                    : Files.newDirectoryStream(dir))) {
                for (Path p : directoryStream) {
                    list.add(p);
                }
            }
            return Collections.unmodifiableList(list);
        }
        return Collections.emptyList();
    }

    /**
     * Tests whether a file is a directory.
     *
     * @param path the path to the file to test.
     * @return true if the file is a directory; false if the file does not
     * exist, is not a directory, or it cannot be determined if the file
     * is a directory or not.
     */
    public static boolean isFolder(Path path) {
        return Files.isDirectory(path);
    }
}

Related

  1. getTables(final List d1)
  2. isBlackListedDirectory(Path path)
  3. isPlaylistFile(Path testFile)
  4. list(final Path directory)
  5. list(Path basePath)
  6. listChildren(Path dir, boolean searchDirectory)
  7. listChildren(Path directory)
  8. listChildren(Path directory)
  9. listDir(Path dir)