Java Path File List nio listChildren(Path dir, boolean searchDirectory)

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

Description

Gets list of children folder or files for dir, according to searchDirectory param.

License

Open Source License

Parameter

Parameter Description
dir folder to search.
searchDirectory if true method return list of folders, otherwise list of files.

Exception

Parameter Description
IOException when IO Exception occurs.

Return

list of files or subdirectories in selected directory

Declaration

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

Method Source Code


//package com.java2s;
/*/*from w  w  w . j a va2  s.c  o  m*/
 * CKFinder
 * ========
 * http://cksource.com/ckfinder
 * Copyright (C) 2007-2015, CKSource - Frederico Knabben. All rights reserved.
 *
 * The software, this file and its contents are subject to the CKFinder
 * License. Please read the license.txt file before using, installing, copying,
 * modifying or distribute this file or part of its contents. The contents of
 * this file is part of the Source Code of CKFinder.
 */

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

import java.util.List;

import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class Main {
    /**
     * Gets list of children folder or files for dir, according to searchDirectory
     * param.
     *
     * @param dir folder to search.
     * @param searchDirectory if true method return list of folders, otherwise
     * list of files.
     * @return list of files or subdirectories in selected directory
     * @throws IOException when IO Exception occurs.
     */
    public static List<Path> listChildren(Path dir, boolean searchDirectory) throws IOException {
        DirectoryStream.Filter<Path> filter = searchDirectory ? Files::isDirectory : Files::isRegularFile;
        try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir, filter)) {
            return StreamSupport.stream(ds.spliterator(), false).collect(Collectors.toList());
        }
    }
}

Related

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