File类中提供listFile()方法,可以获得当前文件夹中包含的全部子文件夹和文件,通过地柜可以获得全部文件

 

import java.io.File;
public class Test1 {
 public static void main(String[] args) {
  String path="d:/";
  File file=new File(path);
  File[] tempList = file.listFiles();
  System.out.println("该目录下对象个数:"+tempList.length);
  for (int i = 0; i < tempList.length; i++) {
   if (tempList[i].isFile()) {
    System.out.println("文     件:"+tempList[i]);
   }
   if (tempList[i].isDirectory()) {
    System.out.println("文件夹:"+tempList[i]);
   }
  }
 }
}

Java中读取某个目录下的所有文件和文件夹