当前位置:编程学习 > 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 © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,