码迷,mamicode.com
首页 > 编程语言 > 详细

Java递归目录结构

时间:2016-06-08 18:53:31      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

import java.io.File;

public class FileTree {

    public static void main(String[] args) {
        printFileTree("D:\\AAAAASSOCWEBDATA",0);
    }

    public static void printFileTree(String driver, int level) {
        File file = new File(driver);
        printByLevel(file.getName(),level);
        if (file.exists()) {
            if(file.isDirectory()){
                File[] children = file.listFiles();
                for (int i = 0; i < children.length; i++) {
                    printFileTree(children[i].getPath(),level+1);
                }
            }
        }
    }

    public static void printByLevel(String name,int level) {
        for (int i = 0; i < level; i++) {
            System.out.print(" ");
        }
        System.out.println("|-?"+name);
    }

}
结果如下
|-?AAAAASSOCWEBDATA |-?bom |-?320923198906262416 |-?320923198906262416.jpg |-?81696e7ae8034cafa3d1c96d9c10f04f.jpg |-?f395ef2b4038442a9b97e1c54362be0c.jpg |-?contact |-?t.docx |-?video

 

Java递归目录结构

标签:

原文地址:http://www.cnblogs.com/doublejun/p/5570879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!