4. 编写一个程序,要求以树状结构展现特定的文件夹及其子文件(夹)

import java.io.*;
public class Test {
	public static void main(String[] args) {
		File f = new File("E:/Java/");
		System.out.println(f.getName());
		tree(f,1);
	}
	public static void tree(File f, int level) { 
		String prestr = " ";
		for(int i=0;i<level;i++){
			prestr +="      ";
		}
		File childs[] = f.listFiles();
		for(int i=0;i<childs.length;i++){
			System.out.println(prestr+childs[i].getName());
			if(childs[i].isDirectory()){
				tree(childs[i],level+1);
			}
		}
	}
}

运行结果:

Java
       aa.txt
       ARE
             dd.txt
             ee.txt
             myfile.txt
       bb.txt
       cc.txt
       HAO
             Dictionaries
                   ak.docx
                   en-US-6-1.bdic
                   ggg.zip
             ff.txt

posted on 2017-04-25 16:24  牧羊人的世界  阅读(123)  评论(0编辑  收藏  举报