Java递归实现遍历文件目录(所有文件夹和子文件)

Java递归实现遍历文件目录(所有文件夹和子文件)
 3  import  java.io.File;
 4 
 5  public   class  ListAllPath {    
 6       public    void  print(File mFile,  int  mlevel){
 7           for ( int  i  =   0 ; i  <  mlevel; i ++ ){
 8              System.out.print( " \t " );
 9          }
10           if  (mFile.isDirectory()){            
11              System.out.println( " < "   +  getPath(mFile)  +   " > " );    
12              String[] str  =  mFile.list();
13               for  ( int  i  =   0 ; i  <  str.length; i ++ ){
14                  print( new  File(mFile.getPath()  +   " \\ "   +  str[i]) , mlevel  +   1 );
15              }            
16          } else {
17              System.out.println(getPath(mFile));
18          }        
19      }
20      
21       public   String  getPath(File mFile){
22          String fullPath  =  mFile.getPath();
23          String[] str  =  fullPath.split( " \\\\ " );
24           return  str[str.length  -   1 ];
25      }
26      
27  }

 1  import  java.io.File;
 2 
 3  public   class  Demo {
 4       public   static   void  main(String[] args){
 5          ListAllPath demoTest  =   new  ListAllPath();
 6          File rootFile  =   new  File( " E:\\job " );
 7          demoTest.print(rootFile,  0 );
 8      }
 9  }
10 

你可能感兴趣的:(Java递归实现遍历文件目录(所有文件夹和子文件))