【JAVA】读取一个文件夹下的所有文件和子文件夹里的子文件

递归方法

用到了File类的exist,isFile,listFiles,getPath,getName

 1     public static void findall(File file ) { 
 2         if(!file.exists())System.out.print("文件不存在!");
 3         if(file.isFile()) {
 4             System.out.println(file.getPath()+" "+file.getName());
 5         }else {
 6             File[] files=file.listFiles();
 7             if(files.length==0) {
 8                 System.out.println("no file");
 9             }else {
10                 for(File f:files) {
11                     findall(f);
12                 }
13             }
14         }
15     }
16     public static void main(String[] args) throws IOException {
17         findall(new File("E://PYTHON"));//(叛徒!
18     }    

 

posted @ 2020-04-07 23:51  盖世阿萌  阅读(1652)  评论(0)    收藏  举报