import java.io.File;import java.io.IOException;public class Main { public static void main(String[] args) { Tools.listFiles(new File(".")); }}class Tools { public static void listFiles(File dir) { list(dir, 0); } private static void list(File file, int depth) { int i = depth; if (file.isFile()) { while (i-- > 0) { System.out.print("\t"); } System.out.println(file.getName()); i = depth; } else if (file.isDirectory()) { while (i-- > 0) { System.out.print("\t"); } try { System.out.println(file.getCanonicalPath() + "/"); } catch (IOException e) { e.printStackTrace(); } for (File f : file.listFiles()) { list(f, depth + 1); } } else { new Exception("不是文件或文件夹!").printStackTrace(); } }}
Sign in to make a reply
苏慕空16895