编写一个程序,在命令行中以树状结构显示特定的文件夹及其子文件(夹)。
------解决方案--------------------
import java.io.File;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String path = readPath();
File file = new File(path);
if(file.exists()){
output(file, 0);
}else{
System.out.print( " *** ");
System.out.print(path + " 不存在!!! ");
System.out.println( " *** ");
}
}
private static String readPath(){
Scanner scanner = new Scanner(System.in);
System.out.print( "请输入文件夹(目录)路径: ");
String path = scanner.nextLine();
return path;
}
private static void output(File file, int k){
for(int i=0; i <k; i++){
System.out.print( " ");
}
System.out.println(file.getName());
if(file.isDirectory()){
File[] files = file.listFiles();
for(File f:files){
output(f, k+1);
}
}
}
}
不知道楼主说的是这个意思吗?