Java使用递归检索文件个数
使用到了Java.io中的File类
File类常见方法:
1.boolen exists()
2.String getName()
3.boolean isDirectory()
4.boolean isFile()
5.File[] listFiles()
import java.io.*; /* 打印某个目录下所有文件和目录的名字 */ public class FileDisplay { public static void main(String[] args) throws Exception{ FileDisplay fd=new FileDisplay(); String dirName="G:\\Users\\14616\\Pictures\\古墓丽影"; int count=fd.printDir(dirName); System.out.println("文件数"+count); } public int printDir(String dirName){ int fileCount=0; File dirFile=new File(dirName); File[] subFile=dirFile.listFiles(); System.out.println("fileLength"+subFile.length); if(subFile==null||subFile.length==0) return 0; for(int i=0;i<subFile.length;i++){ if(subFile[i].isDirectory()){ String subDir=subFile[i].getAbsolutePath(); System.out.println("目录"+subDir); fileCount+=printDir(subDir); } if(subFile[i].isFile()){ fileCount++; String fileName=subFile[i].getAbsolutePath(); System.out.println("文件"+fileName); } } return fileCount; } }
输出
"G:\Program Files\Java\jdk1.8.0_121\bin\java" "-javaagent:G:\JetBrains\IntelliJ IDEA 2017.1.4\lib\idea_rt.jar=58448:G:\JetBrains\IntelliJ IDEA 2017.1.4\bin" -Dfile.encoding=UTF-8 -classpath "G:\Program Files\Java\jdk1.8.0_121\jre\lib\charsets.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\deploy.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\javaws.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\jce.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\jfr.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\jsse.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\management-agent.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\plugin.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\resources.jar;G:\Program Files\Java\jdk1.8.0_121\jre\lib\rt.jar;H:\practice\out\production\practice" FileDisplay fileLength8 文件G:\Users\14616\Pictures\古墓丽影\image_rise_of_the_tomb_raider-27559-2982_0010.jpg 文件G:\Users\14616\Pictures\古墓丽影\image_rise_of_the_tomb_raider-27559-2982_0011.jpg 文件G:\Users\14616\Pictures\古墓丽影\image_rise_of_the_tomb_raider-27559-2982_0012.jpg 文件G:\Users\14616\Pictures\古墓丽影\image_rise_of_the_tomb_raider-28633-2982_0001.jpg 文件G:\Users\14616\Pictures\古墓丽影\image_rise_of_the_tomb_raider-29110-2982_0001.jpg 文件G:\Users\14616\Pictures\古墓丽影\image_rise_of_the_tomb_raider-29110-2982_0002.jpg 目录G:\Users\14616\Pictures\古墓丽影\新建文件夹 fileLength5 文件G:\Users\14616\Pictures\古墓丽影\新建文件夹\image_rise_of_the_tomb_raider-25298-2982_0001.jpg 文件G:\Users\14616\Pictures\古墓丽影\新建文件夹\image_rise_of_the_tomb_raider-25298-2982_0002.jpg 文件G:\Users\14616\Pictures\古墓丽影\新建文件夹\image_rise_of_the_tomb_raider-25298-2982_0003.jpg 文件G:\Users\14616\Pictures\古墓丽影\新建文件夹\image_rise_of_the_tomb_raider-27559-2982_0001.jpg 文件G:\Users\14616\Pictures\古墓丽影\新建文件夹\image_rise_of_the_tomb_raider-27559-2982_0007.jpg 目录G:\Users\14616\Pictures\古墓丽影\新建文件夹 (2) fileLength9 文件G:\Users\14616\Pictures\古墓丽影\新建文件夹 (2)\image_rise_of_the_tomb_raider-29110-2982_0003.jpg 文件G:\Users\14616\Pictures\古墓丽影\新建文件夹 (2)\image_rise_of_the_tomb_raider-29110-2982_0006.jpg 文件G:\Users\14616\Pictures\古墓丽影\新建文件夹 (2)\image_rise_of_the_tomb_raider-30524-2982_0003.jpg 文件G:\Users\14616\Pictures\古墓丽影\新建文件夹 (2)\image_rise_of_the_tomb_raider-30659-2982_0005.jpg 文件G:\Users\14616\Pictures\古墓丽影\新建文件夹 (2)\image_rise_of_the_tomb_raider-30659-2982_0006.jpg 文件G:\Users\14616\Pictures\古墓丽影\新建文件夹 (2)\image_rise_of_the_tomb_raider-30659-2982_0010.jpg 文件G:\Users\14616\Pictures\古墓丽影\新建文件夹 (2)\image_rise_of_the_tomb_raider-30659-2982_0011.jpg 文件G:\Users\14616\Pictures\古墓丽影\新建文件夹 (2)\image_rise_of_the_tomb_raider-30659-2982_0016.jpg 文件G:\Users\14616\Pictures\古墓丽影\新建文件夹 (2)\image_rise_of_the_tomb_raider-30659-2982_0017.jpg 文件数20
需要注意的是:
listFiles方法返回的对象数组的长度是当前目录下文件和子目录的总数,而不是总文件的个数。