递归算法java

package test;

import java.io.File;
import java.io.IOException;

public class FileTest {
    public static void main(String[] args) {
        String path="d:\\aaa";
        File f=new File(path);
        showFile(f);
        
    }
    
    //递归算法
    //返回当前目录下的所有文件 对象形式
    public static void showFile(File f){
        
        File arr[]=f.listFiles();
        if(arr.length==0){
            System.out.println(f.getAbsolutePath());  //获取f绝对路径

        }else{
            for(File ftemp:arr){
                if(ftemp.isDirectory()){    //如果还是一个文件夹,那么调用自己来继续执行判断File是否是目录,是目录返回true
                    showFile(ftemp);
                }else{
                    System.out.println(ftemp.getAbsolutePath());
                }
                
            }
        }
        
    }

}

 

 

}

posted @ 2016-09-22 16:57  IT~天空  阅读(104)  评论(0编辑  收藏  举报