package javatext;
import java.io.File;
public class ShowFileTree {
public static void main(String[] args) {
File f = new File("D:/workspace/j2setest/src/javatext");
System.out.println(f.getName());
showTree(f,1);
}
//显示目录树结构
public static void showTree(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++){
String fileName = childs.getName();
if(getExession(fileName).equals("java")){
System.out.println(preStr+childs.getName());
}
if(childs.isDirectory()){
showTree(childs, level+1);
}
}
}
//返回文件的后缀名
public static String getExession(String fileName){
String[] str = fileName.split("\\.");
return str[str.length-1];
}
}
|