06_1_列出文件目录树

时间:2022-11-11 12:42:32

06_1_列出文件目录树

1. 例子

package Test;

import java.io.File;

public class TestFileList {

	public static void main(String[] args) {
		File f = new File("D:/桌面/桌面/01开发/项目管理");
	/*	for (int i = 0; i < f.listFiles().length; i++) {
			System.out.println(f.listFiles()[i]);
			for (int j = 0; j < new File(f.listFiles()[i]+"").listFiles().length; j++) {
				System.out.println("   " + new File(f.listFiles()[i]+"").listFiles()[j] );
			}
		}*/
		tree(f, 0);
	}
	
	public static void tree(File f, int level) {
		File[] children = f.listFiles();
		String str = "";
		for (int i = 0; i < level; i++) {
			str += "    ";
		}
		for (int i = 0; i < children.length; i++) {
			
			System.out.println(str + children[i].getName());
			if(children[i].isDirectory()) {
				tree(children[i], level+1);
			}
		}
	}
}