List all subdirectories present a directory in Java
This post will discuss how to list all subdirectories present a directory in Java.
1. Using File#list() method
The File#list() method is used to get the files and directories in a directory that satisfy the specified file name filter. You can override the accept() method of the FilenameFilter that returns true if the specified file should be included in the file list; false otherwise. To list all subdirectories, return true if and only if the file is a directory.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.io.File; import java.io.FilenameFilter; import java.util.Arrays; public class Main { public static void main(String[] args) { String path = "/path/to/dir"; File file = new File(path); String[] directories = file.list(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return new File(dir, name).isDirectory(); } }); System.out.println(Arrays.toString(directories)); } } |
The above code can be further shortened with Java 8 lambdas, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.io.File; import java.util.Arrays; public class Main { public static void main(String[] args) { String path = "/path/to/dir"; File file = new File(path); String[] directories = file.list((dir, name) -> new File(dir, name).isDirectory()); System.out.println(Arrays.toString(directories)); } } |
2. Using File#listFiles() method
You can also use the File#listFiles() method to get a pathname list of the files and directories in the specified directory that satisfy the specified file filter. You can override the accept() method of the FileFilter that checks whether the supplied pathname should be included in the output.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.io.File; import java.io.FileFilter; import java.util.Arrays; public class Main { public static void main(String[] args) { String path = "/path/to/dir"; File[] directories = new File(path).listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory(); } }); System.out.println(Arrays.toString(directories)); } } |
The above code is equivalent to the following, which uses method reference:
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.io.File; import java.util.Arrays; public class Main { public static void main(String[] args) { String path = "/path/to/dir"; File[] directories = new File(path).listFiles(File::isDirectory); System.out.println(Arrays.toString(directories)); } } |
3. Using DirectoryStream
Alternatively, you can use the DirectoryStream to iterate over the files in a directory that satisfy some condition. Here’s a simple example of its usage to filter only directories:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; public class Main { public static void main(String[] args) { String path = "/path/to/dir"; try (DirectoryStream<Path> ds = Files.newDirectoryStream( FileSystems.getDefault().getPath(path), Files::isDirectory)) { for (Path p : ds) { System.out.println(p.getFileName()); } } catch (IOException e) { e.printStackTrace(); } } } |
4. Using Files.walk() method
If you need to limit the maximum number of directory levels to visit, consider using the Files.walk() method that returns a Stream<Path>. It is available since JDK 1.8. To visit all levels, you can pass Integer.MAX_VALUE value.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) throws IOException { String path = "/path/to/dir"; List<Path> dirs = Files.walk(Paths.get(path), 1) .filter(Files::isDirectory) .collect(Collectors.toList()); System.out.println(dirs); } } |
That’s all about listing all subdirectories present a directory in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)