/*
时间:2014年12月22日21:12:44
功能:打印文件(夹)路径及其子文件(夹)路径(递归)
*/
import java.io.*;
public class FileList {
public static void main(String[] args) {
File f = new File("e:/A");
System.out.println(f.getName());
tree(f, 1);
}
private static void tree(File f, int level) { //level用于区别第几层目录
String preStr = ""; //文件名前面加空格
for(int i=0; i<level; i++) {
preStr += " ";
}
File[] children = f.listFiles();
for(int i=0; i<children.length; i++) { //这个length怎么不写成length() ??
System.out.println(preStr + children[i].getName()); //文件名前面加空格
if(children[i].isDirectory()) {
tree(children[i], level + 1); //level + 1 和 level ++ 区别在哪??
}
}
}
}
level++输出结果:
level+1输出结果:
/*方法(from API):
getName()
public String getName()
返回由此抽象路径名表示的文件或目录的名称。
isDirectory()
public boolean isDirectory()
测试此抽象路径名表示的文件是否是一个目录。
*/
to be continued ...