使用到了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 FilesJavajdk1.8.0_121injava" "-javaagent:G:JetBrainsIntelliJ IDEA 2017.1.4libidea_rt.jar=58448:G:JetBrainsIntelliJ IDEA 2017.1.4in" -Dfile.encoding=UTF-8 -classpath "G:Program FilesJavajdk1.8.0_121jrelibcharsets.jar;G:Program FilesJavajdk1.8.0_121jrelibdeploy.jar;G:Program FilesJavajdk1.8.0_121jrelibextaccess-bridge-64.jar;G:Program FilesJavajdk1.8.0_121jrelibextcldrdata.jar;G:Program FilesJavajdk1.8.0_121jrelibextdnsns.jar;G:Program FilesJavajdk1.8.0_121jrelibextjaccess.jar;G:Program FilesJavajdk1.8.0_121jrelibextjfxrt.jar;G:Program FilesJavajdk1.8.0_121jrelibextlocaledata.jar;G:Program FilesJavajdk1.8.0_121jrelibext ashorn.jar;G:Program FilesJavajdk1.8.0_121jrelibextsunec.jar;G:Program FilesJavajdk1.8.0_121jrelibextsunjce_provider.jar;G:Program FilesJavajdk1.8.0_121jrelibextsunmscapi.jar;G:Program FilesJavajdk1.8.0_121jrelibextsunpkcs11.jar;G:Program FilesJavajdk1.8.0_121jrelibextzipfs.jar;G:Program FilesJavajdk1.8.0_121jrelibjavaws.jar;G:Program FilesJavajdk1.8.0_121jrelibjce.jar;G:Program FilesJavajdk1.8.0_121jrelibjfr.jar;G:Program FilesJavajdk1.8.0_121jrelibjfxswt.jar;G:Program FilesJavajdk1.8.0_121jrelibjsse.jar;G:Program FilesJavajdk1.8.0_121jrelibmanagement-agent.jar;G:Program FilesJavajdk1.8.0_121jrelibplugin.jar;G:Program FilesJavajdk1.8.0_121jrelib esources.jar;G:Program FilesJavajdk1.8.0_121jrelib t.jar;H:practiceoutproductionpractice" FileDisplay fileLength8 文件G:Users14616Pictures古墓丽影image_rise_of_the_tomb_raider-27559-2982_0010.jpg 文件G:Users14616Pictures古墓丽影image_rise_of_the_tomb_raider-27559-2982_0011.jpg 文件G:Users14616Pictures古墓丽影image_rise_of_the_tomb_raider-27559-2982_0012.jpg 文件G:Users14616Pictures古墓丽影image_rise_of_the_tomb_raider-28633-2982_0001.jpg 文件G:Users14616Pictures古墓丽影image_rise_of_the_tomb_raider-29110-2982_0001.jpg 文件G:Users14616Pictures古墓丽影image_rise_of_the_tomb_raider-29110-2982_0002.jpg 目录G:Users14616Pictures古墓丽影新建文件夹 fileLength5 文件G:Users14616Pictures古墓丽影新建文件夹image_rise_of_the_tomb_raider-25298-2982_0001.jpg 文件G:Users14616Pictures古墓丽影新建文件夹image_rise_of_the_tomb_raider-25298-2982_0002.jpg 文件G:Users14616Pictures古墓丽影新建文件夹image_rise_of_the_tomb_raider-25298-2982_0003.jpg 文件G:Users14616Pictures古墓丽影新建文件夹image_rise_of_the_tomb_raider-27559-2982_0001.jpg 文件G:Users14616Pictures古墓丽影新建文件夹image_rise_of_the_tomb_raider-27559-2982_0007.jpg 目录G:Users14616Pictures古墓丽影新建文件夹 (2) fileLength9 文件G:Users14616Pictures古墓丽影新建文件夹 (2)image_rise_of_the_tomb_raider-29110-2982_0003.jpg 文件G:Users14616Pictures古墓丽影新建文件夹 (2)image_rise_of_the_tomb_raider-29110-2982_0006.jpg 文件G:Users14616Pictures古墓丽影新建文件夹 (2)image_rise_of_the_tomb_raider-30524-2982_0003.jpg 文件G:Users14616Pictures古墓丽影新建文件夹 (2)image_rise_of_the_tomb_raider-30659-2982_0005.jpg 文件G:Users14616Pictures古墓丽影新建文件夹 (2)image_rise_of_the_tomb_raider-30659-2982_0006.jpg 文件G:Users14616Pictures古墓丽影新建文件夹 (2)image_rise_of_the_tomb_raider-30659-2982_0010.jpg 文件G:Users14616Pictures古墓丽影新建文件夹 (2)image_rise_of_the_tomb_raider-30659-2982_0011.jpg 文件G:Users14616Pictures古墓丽影新建文件夹 (2)image_rise_of_the_tomb_raider-30659-2982_0016.jpg 文件G:Users14616Pictures古墓丽影新建文件夹 (2)image_rise_of_the_tomb_raider-30659-2982_0017.jpg 文件数20
需要注意的是:
listFiles方法返回的对象数组的长度是当前目录下文件和子目录的总数,而不是总文件的个数。