文件递归查找

 1 public class BianLiFile {
 2     public static void main(String[] args) {
 3         //删除或者重命名时只能对单级目录进行操作
 4         getFile("D:\\");
 5     }
 6 
 7     //分多次去操作
 8     //获取多级目录下的所有文件
 9     public static void getFile(String path){
10         File file = new File(path);
11         //如果只是一个文件或者不存在,就返回
12         if (file.isFile()||!file.exists()){
13             //如果不是文件或者不存在
14             return ;
15         }
16         File[] files = file.listFiles();
17         for (File file1 : files) {//如果是一個文件
18             if (file1.isFile()){
19                 System.out.println("文件"+file1.getName());
20             }
21             else{
22                 System.out.println("目录:"+file1.getName());
23                 getFile(file1.getPath());
24 
25             }
26         }
27     }
28 }

猜你喜欢

转载自www.cnblogs.com/rzkwz/p/12632903.html