使用这段代码
new File("/mnt/sdcard/folder").listFiles().length
返回特定目录中文件夹和文件的总和,而不关心子目录。 我想获取目录及其子目录中所有文件的数量。
附言: 如果它返回所有文件和文件夹的总和,则无关紧要。
感谢任何帮助, 谢谢
最佳答案
试试这个。
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());
}
}
}
它可能对你有帮助。
关于java - 获取目录及其子目录中的文件数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12616124/