完整代码:https://github.com/tonygeegle/FileTree
private void showTree(File entry, int depth, int order, int brothers) { String name = entry.getAbsolutePath(); if (depth > 0) { //根据当前节点的深度,打印相应数量的空格进行缩进 for (int i = 1; i < depth; i++) { int num = this.indentNum; // 判断是否打印竖线。这里好像比较复杂,应该进行回溯,单独编写个方法。 if (isVerticalLine(entry, i, depth)) { System.out.print(this.lines[2]); num--; } for (int j = 0; j < num; j++) { System.out.print(indentChar); } } //根据当前节点是否是最后一个节点,打印"└─ "或者"├─ " System.out.print(lines[brothers - order == 0 ? 0: 1]); //根据当前节点的深度,调整打印名字,而不是绝对路径 name = entry.getName(); } System.out.println(name); if(entry.isDirectory()) { File[] children = entry.listFiles(new filter()); for (int i = 0; i < children.length; i++) { showTree(children[i], depth + 1, i, children.length - 1); } } }
Sign in to make a reply
dfdfdsadf
完整代码:https://github.com/tonygeegle/FileTree