public class Main { public static void main(String[] args) throws IOException { //指定 文件夹 路径 File currentDir = new File("./"); System.out.println(currentDir.getCanonicalFile()); listDir(currentDir.getCanonicalFile()); } static int index = 0; static String s = ""; static void listDir(File dir) { File[] fs = dir.listFiles(); if (fs != null) { for (File f : fs) { if (f.isDirectory()) { index++; s = ""; for (int i = 0; i < index; i++) { s += "-"; } System.out.println(s + f.getName() + "\\"); listDir(f); index--; continue; } s = ""; for (int i = 0; i < index; i++) { s += "-"; } System.out.println(s + "-" + f.getName()); } } }}
ANGERIED
我分析了一下原因,因为一开始就打算用计步器来记录层数,然后想打印成上面的“-”,结果发现代码越写越越多,但是还是硬着头写完了。接着对比了大佬们的代码,恍然大悟,原来可以在计数器那里做优化,然后学大佬在文件夹后加了个“\”,结果变得清晰很多,但是离标准标准答案还有一点距离,继续努力吧。