Java, listing only subdirectories from a directory, not files

In Java, how do I display only subdirectories from a directory?

I would like to use java.io.File functionality, what is the best method in Java for this?

+82
java file
Feb 26 '11 at 5:23
source share
9 answers

You can use the File class to display directories.

File file = new File("/path/to/directory"); String[] directories = file.list(new FilenameFilter() { @Override public boolean accept(File current, String name) { return new File(current, name).isDirectory(); } }); System.out.println(Arrays.toString(directories)); 

Update

The author’s comment on this post wanted a faster way, a great discussion here: How to get a directory listing FAST in Java?

Basically:

  • If you control the structure of the file, I will try to avoid getting into this situation.
  • In Java NIO.2, you can use the directory function to return an iterator to provide greater scalability. A directory stream class is an object that can be used to iterate over entries in a directory.
+121
Feb 26 '11 at 5:27
source share

A very simple solution for Java 8:

 File[] directories = new File("/your/path/").listFiles(File::isDirectory); 

This is equivalent to using FileFilter (also works with old Java):

 File[] directories = new File("/your/path/").listFiles(new FileFilter() { @Override public boolean accept(File file) { return file.isDirectory(); } }); 
+92
Jan 14 '15 at 10:12
source share

@Mohamed Mansour, you were almost there ... the argument "dir" from what you used is actually an empty path, so it will always return true. To find out if the child file is a subdirectory or not, you need to check this child.

 File file = new File("/path/to/directory"); String[] directories = file.list(new FilenameFilter() { @Override public boolean accept(File current, String name) { return new File(current, name).isDirectory(); } }); System.out.println(Arrays.toString(directories)); 
+13
Aug 19 '11 at 23:50
source share

If you are interested in a solution using Java 7 and NIO.2, it might look like this:

 private static class DirectoriesFilter implements Filter<Path> { @Override public boolean accept(Path entry) throws IOException { return Files.isDirectory(entry); } } try (DirectoryStream<Path> ds = Files.newDirectoryStream(FileSystems.getDefault().getPath(root), new DirectoriesFilter())) { for (Path p : ds) { System.out.println(p.getFileName()); } } catch (IOException e) { e.printStackTrace(); } 
+7
Jul 13 '13 at 9:53 on
source share

For those who are also interested in Java 7 and NIO, there is an alternative solution for @voo's answer above . We can use try-with-resources, which calls Files.find() and the lambda function, which is used to filter directories.

 import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.stream.Stream; final Path directory = Paths.get("/path/to/folder"); try (Stream<Path> paths = Files.find(directory, Integer.MAX_VALUE, (path, attributes) -> attributes.isDirectory())) { paths.forEach(System.out::println); } catch (IOException e) { ... } 

We can even filter directories by name by changing the lambda function:

 (path, attributes) -> attributes.isDirectory() && path.toString().contains("test") 

or by date:

 final long now = System.currentTimeMillis(); final long yesterday = new Date(now - 24 * 60 * 60 * 1000L).getTime(); // modified in the last 24 hours (path, attributes) -> attributes.isDirectory() && attributes.lastModifiedTime().toMillis() > yesterday 
+1
Aug 01 '18 at 23:31
source share

Given the start directory as String

  • Create a method that takes a String path as a parameter. In the method:
  • Create a new File object based on the start directory
  • Get an array of files in the current directory using the listFiles method
  • Iterate over an array of files
    • If this is a file, continue the loop
    • If it is a directory, print the name and recursion on this new directory path
0
Feb 26 '11 at 5:29
source share

Here is the solution for my code. I just made a small change from the first answer . This will list all folders only in the desired directory line by line:

 try { File file = new File("D:\\admir\\MyBookLibrary"); String[] directories = file.list(new FilenameFilter() { @Override public boolean accept(File current, String name) { return new File(current, name).isDirectory(); } }); for(int i = 0;i < directories.length;i++) { if(directories[i] != null) { System.out.println(Arrays.asList(directories[i])); } } }catch(Exception e) { System.err.println("Error!"); } 
0
Apr 29 '18 at 9:45
source share

The solution that worked for me is not in the answer list. Therefore, I am posting this solution here:

 File[]dirs = new File("/mypath/mydir/").listFiles((FileFilter)FileFilterUtils.directoryFileFilter()); 

Here I used org.apache.commons.io.filefilter.FileFilterUtils from Apache commons-io-2.2.jar. Its documentation is available here: https://commons.apache.org/proper/commons-io/javadocs/api-2.2/org/apache/commons/io/filefilter/FileFilterUtils.html.

0
Mar 15 '19 at 10:44
source share
 ArrayList<File> directories = new ArrayList<File>( Arrays.asList( new File("your/path/").listFiles(File::isDirectory) ) ); 
0
Jun 10 '19 at 15:04 on
source share



All Articles