get number of files in a directory and its subdirectories

后端 未结 12 890
猫巷女王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 09:54

    You have to go though all the folder recursively and find out the files

    int mCount;
    
    getTotalFiles(File dir) {
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                getTotalFiles(file);
            } else {
                mCount++;
            }
        }
    }
    

提交回复
热议问题