java分享第七天-03(递归打印文件目录的树状结构)

时间:2022-05-19 22:11:49
    public static void main(String[] args) {
    File file= new File("e:/list");
    printFile(file, 0);
    }
    static void printFile(File file ,int level){
        for (int i = 0; i <level; i++) {
            System.out.print("-");
        }
        System.out.println(file.getName());
        if (file.isDirectory()) {
            File[] files =file.listFiles();
            for (File temp : files) {
                printFile(temp, level+1);
            }
        }
    }