get number of files in a directory and its subdirectories

后端 未结 12 889
猫巷女王i
猫巷女王i 2020-12-28 09:19

using this code

new File(\"/mnt/sdcard/folder\").listFiles().length

returns a sum of folders and files in a particular directory without ca

12条回答
  •  猫巷女王i
    2020-12-28 10:11

    public Integer countFiles(File folder, Integer count) {
        File[] files = folder.listFiles();
        for (File file: files) {
            if (file.isFile()) {
                count++;
            } else {
                countFiles(file, count);
            }
        }
    
        return count;
    }
    

    Usage:

    Integer count = countFiles(new File("your/path"), Integer.valuOf(0));
    

提交回复
热议问题