In this source code example, we show you how to list all the subdirectories from a directory in Java.
We use Files.list() method to list all the subdirectories from a directory in Java.Java List All Subdirectories From a Directory Example
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
public class FilesListDirectories {
public static void main(String[] args) throws IOException {
var homeDir = System.getProperty("user.home");
Files.list(new File(homeDir).toPath())
.filter(path -> path.toFile().isDirectory())
.forEach(System.out::println);
}
}
References
Related Java NIO Files Examples
- Java File Copy Example
- Java Create File Example
- Java Delete File Example
- Java File Size Example
- Java Get File Owner Example
- Java File Move Example
- Java Read File Example
- Java Write File Example
- Java Create Directory Example
- Java Create Multiple Directories Example
- Java List All Subdirectories From a Directory Example
- Java Append to File Example
Comments
Post a Comment