Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: one-liner to list Dirs in a directory?

Tags:

java

directory

One-Liner to list TXT-files.

import java.io.File;
import java.io.FilenameFilter;
...
files = dir.listFiles(new FilenameFilter() {
           public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".txt");
                }
           }
        );

Source.

Is there an one-liner to list dirs in a dir?

like image 805
hhh Avatar asked Dec 17 '22 01:12

hhh


1 Answers

public static void main (String[] args)  throws Exception {
    File dir = new File("yourDir");

    FileFilter fileFilter = new FileFilter() {
        public boolean accept(File file) {
            return file.isDirectory();
        }
    };

    File[] files  = dir.listFiles(fileFilter);

    for (File f : files)
        System.out.println( f.getName() );
}
like image 142
Wajdy Essam Avatar answered Dec 21 '22 09:12

Wajdy Essam