java递归遍历文件

来源:互联网 发布:软件设计师宁波分数线 编辑:程序博客网 时间:2024/06/06 02:58

this recursive function

Java代码  收藏代码
  1. public class Test {  
  2.   
  3.     public static void main(String[] args) throws IOException {  
  4.         File file = new File("E:\\plan");  
  5.         Test.recursive(file);  
  6.     }  
  7.       
  8.     public static void  recursive(File file)  
  9.     throws IOException {  
  10.     // do not try to index files that cannot be read  
  11.     if (file.canRead()) {  
  12.       if (file.isDirectory()) {  
  13.         String[] files = file.list();  
  14.         // an IO error could occur  
  15.         if (files != null) {  
  16.           for (int i = 0; i < files.length; i++) {  
  17.               recursive(new File(file, files[i]));  
  18.           }  
  19.         }  
  20.       } else {  
  21.         System.out.println("adding " + file);  
  22.       }  
  23.     }  
  24.   }  
  25. }  
 
原创粉丝点击