Recursive directory tree Print

 

First, print out all subdirectories under the directory utilizing the listFiles ( File [] = f.listFiles Childs () ) method and the File class, the isDirectory, conditions and write a recursive call itself recursively as recursive plate

Indent problem using the method of mass participation, increase the level of parameters to distinguish. Note: Use the directory /, and write code gradual improvement.

import java.io.*;


public class FileList {
	public static void main(String[] args) {
		File f = new File("c:/java/A");
		System.out.println(f.getName());
		tree(f,1);
	}
	
	private 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);
			}
		}
	}
}

 

Guess you like

Origin www.cnblogs.com/lsswudi/p/11354481.html