Recursively list files in Java

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

    I would go with something like:

    public void list(File file) {
        System.out.println(file.getName());
        File[] children = file.listFiles();
        for (File child : children) {
            list(child);
        }
    }
    

    The System.out.println is just there to indicate to do something with the file. there is no need to differentiate between files and directories, since a normal file will simply have zero children.

提交回复
热议问题