Here you can find the source of listChildren(Path dir)
Parameter | Description |
---|---|
dir | path to the directory. |
Parameter | Description |
---|---|
IOException | if something goes wrong. |
public static List<Path> listChildren(Path dir) throws IOException
//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); } }