How to find sub-directories in a directory/folder?

前端 未结 3 1467
北荒
北荒 2020-12-18 18:31

I\'m looking for a way to get all the names of directories in a given directory, but not files.

For example, let\'s say I have a folder called Parent, a

3条回答
  •  死守一世寂寞
    2020-12-18 19:07

    You can use String[] directories = file.list() to list all file names, then use loop to check each sub-files and use file.isDirectory() function to get subdirectories.

    For example:

    File file = new File("C:\\Windows");
    String[] names = file.list();
    
    for(String name : names)
    {
        if (new File("C:\\Windows\\" + name).isDirectory())
        {
            System.out.println(name);
        }
    }
    

提交回复
热议问题