1 public class IO02 {
2 public static void main(String[] args) {
3 File file = new File("D:\\JAVA重拾\\test1\\test01");
4 logFileName(file,0);
5 }
6 public static void logFileName(File file,int i) {
7 String str = "";
8 for(int j=0;j<i;j++) { //目录等级
9 str += "-";
10 }
11 System.out.println(str+file.getName());
12 if(file == null || !file.exists()) { //如果目标不存在
13 return ;
14 }else {
15 if(file.isDirectory()) { //如果是文件夹
16 for(File files : file.listFiles()) { //获取遍历其下的文件夹或目录
17 logFileName(files,i++);
18 }
19 }
20 }
21 }
22 }