public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你需要查看的文件夹路径");
String path = sc.nextLine();
String recognizer = ""; //识别器
int count = 1; //计数器
iterator(new File(path),path,recognizer,count);
}
public static void iterator(File file,String path,String recognizer,int count) {
ArrayList<String> list = new ArrayList<String>();
int en = 0;//这里加了这个计数器,是为了让文件与文件夹之间会空格区分(不知道这样的效果好不好)
if (file.isDirectory()) {
System.out.println(recognizer + file.getName() + "......第" + count++ + "级文件夹");
recognizer = recognizer + "\t";
String[] str = file.list();
for (String string : str) {
String repath = path + "\\" + string;
if (new File(repath).isDirectory()) {
list.add(repath);
continue;
}else{
iterator(new File(repath),repath,recognizer,count);
en++;
}
/*String repath = path + "\\" + string;
iterator(new File(repath),repath,recognizer);*/
}
if (en > 0) {
System.out.println("");
}//计数器的作用,如果有文件,就会大于0,就会执行,如果没有文件,只有文件夹就不会执行了
}else{
System.out.println( recognizer + file.getName() + "文件");
return;//加此语句结束方法,防止传入进来的是文件,执行了下面的for语句
}
// System.out.println("");
for (String string : list) {
iterator(new File(string),string,recognizer,count);
}
}
} |
|