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

前端 未结 3 1464
北荒
北荒 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:18

    public static void displayDirectoryContents(File dir) {
        try {
            File[] files = dir.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    System.out.println("Directory Name==>:" + file.getCanonicalPath());
                    displayDirectoryContents(file);
                } else {
                    System.out.println("file Not Acess===>" + file.getCanonicalPath());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    }

    ====inside class/Method provide File=URL ======

        File currentDir = new File("/home/akshya/NetBeansProjects/");
        displayDirectoryContents(currentDir);
    }
    

提交回复
热议问题