Java递归法读取文件目录

任意给一个目录(至少有三层子目录),通过递归方法,将目录及所有子目录下的文件都列出来

import java.io.File;

public class $ {

    public static void main(String[] args) {

        String path = "D:/";

        test(path);
    }

    private static void test(String path) {
        File f = new File(path);

        File[] fs = f.listFiles();

        if (fs == null) {
            return;
        }

        for (File file : fs) {
            if (file.isFile()) {
                System.out.println(file.getPath());
            } else {
                test(file.getPath());
            }
        }
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-10-23
简单的说就是
public void ListFile(File file){
File[] fileList = file.listFiles();
for(int i = 0; i < fileList.length;i++){
if(fileList[i].isDirectory()){
ListFile(fileList[i]);
}else if(fileList[i].isFile()){
System.out.println(fileList[i].getName());
}
}
}
前边加上对参数合法性的判断,里边在加上你要的处理过程。
第2个回答  2015-10-28
你最终需要的是什么样的数据结构!?
相似回答