get number of files in a directory and its subdirectories

后端 未结 12 899
猫巷女王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条回答
  •  旧巷少年郎
    2020-12-28 10:05

    Try this.

    int count = 0;
    getFile("/mnt/sdcard/folder/");
    
    private void getFile(String dirPath) {
        File f = new File(dirPath);
        File[] files = f.listFiles();
    
        if (files != null)
        for (int i = 0; i < files.length; i++) {
            count++;
            File file = files[i];
    
            if (file.isDirectory()) {   
                 getFile(file.getAbsolutePath()); 
            }
        }
    }
    

    It may help you.

提交回复
热议问题