Recursively list files in Java

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

    just write it yourself using simple recursion:

    public List addFiles(List files, File dir)
    {
        if (files == null)
            files = new LinkedList();
    
        if (!dir.isDirectory())
        {
            files.add(dir);
            return files;
        }
    
        for (File file : dir.listFiles())
            addFiles(files, file);
        return files;
    }
    

提交回复
热议问题