Java-J2SE学习笔记-树状展现文件结构

1.利用java.io相关类树状展现文件结构

2.判定给定路径是否为dir,是则递归,每一递归一层缩进一次

3.代码

package Test;

import java.io.File;

public class TestHierarchical {

	public static void main(String[] args) {
		File file = new File("D:/Workspaces/eclipse/test");
		tree(file, 0);
	}

	private static void tree(File f, int level) {
	
		String preStr = "";
		for(int i = 0 ; i < level ; i++){
			preStr +="---";
		}
		
		File [] fs = f.listFiles();
		for(File tmp : fs){
			System.out.println(preStr+tmp.getName());
			if(tmp.isDirectory()){
				tree(tmp, level+1);
			}
		}
	}
}

  

4.运行结果:

posted @ 2015-06-28 11:16  shamgod  阅读(240)  评论(0编辑  收藏  举报
haha