得到一个目录及其子目录中的文件的数量 [英] get number of files in a directory and its subdirectories

查看:120
本文介绍了得到一个目录及其子目录中的文件的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此code

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

返回文件夹和文件的特定目录的款项无需关心子目录。 我想在一个目录及其子目录中的所有文件数。

returns a sum of folders and files in a particular directory without caring about subdirectories. I want to get number of all files in a directory and its subdirectories.

P.S。 :并不重要,如果它返回的所有文件和文件夹的总和

P.S. : hardly matters if it returns a sum of all the files and folders.

任何帮助AP preciated, 谢谢

any help appreciated, thanks

推荐答案

试试这个。

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.

这篇关于得到一个目录及其子目录中的文件的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆