Recursively list files in Java

后端 未结 27 1598
走了就别回头了
走了就别回头了 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:40

    This code is ready to run

    public static void main(String... args) {
        File[] files = new File("D:/").listFiles();
        if (files != null) 
           getFiles(files);
    }
    
    public static void getFiles(File[] files) {
        for (File file : files) {
            if (file.isDirectory()) {
                getFiles(file.listFiles());
            } else {
                System.out.println("File: " + file);
            }
        }
    }
    

提交回复
热议问题