当前位置:编程学习 > JAVA >>

Java递归实现遍历文件目录

import java.io.File;
  public class ListAllPath {   
      public  void print(File mFile, int mlevel){
         for(int i = 0; i < mlevel; i++){
             System.out.print("\t");
         }
        if (mFile.isDirectory()){           
             System.out.println("<" + getPath(mFile) + ">");   
            String[] str = mFile.list();
            for (int i = 0; i < str.length; i++){
                print(new File(mFile.getPath() + "\\" + str[i]) , mlevel + 1);
            }           
         }else{
             System.out.println(getPath(mFile));
        }       
    }
   
    public  String  getPath(File mFile){
        String fullPath = mFile.getPath();
        String[] str = fullPath.split("\\\\");
         return str[str.length - 1];
     }
    
 }

 import java.io.File;
 
 public class Demo {
     public static void main(String[] args){
          ListAllPath demoTest = new ListAllPath();
          File rootFile = new File("E:\\job");
          demoTest.print(rootFile, 0);
      }
  }
 

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,