package IO.file.test; import java.io.File; public class FileTest { public static void main(String[] args) { File dir=new File("f:\\javaspring"); listAll(dir,0); } public static void listAll(File dir,int level) { //获取指定目录下的所有 System.out.println(getSpace(level)+"dir"+dir.getName()); level++; File[] files=dir.listFiles(); for(int x=0;x<files.length;x++) { if(files[x].isDirectory()){ listAll(files[x],level); } else System.out.println(getSpace(level)+files[x].getName()); } } private static String getSpace(int level) { StringBuilder sb=new StringBuilder(); sb.append("!"); for(int x=0;x<level;x++) { sb.append("---"); } return sb.toString(); } }