Recursively list files in Java

后端 未结 27 2027
走了就别回头了
走了就别回头了 2020-11-22 00:29

How do I recursively list all files under a directory in Java? Does the framework provide any utility?

I saw a lot of hacky implementations. But none from the fra

27条回答
  •  情深已故
    2020-11-22 00:50

    I came up with this for printing all the files/file names recursively.

    private static void printAllFiles(String filePath,File folder) {
        if(filePath==null) {
            return;
        }
        File[] files = folder.listFiles();
        for(File element : files) {
            if(element.isDirectory()) {
                printAllFiles(filePath,element);
            } else {
                System.out.println(" FileName "+ element.getName());
            }
        }
    }
    

提交回复
热议问题