|
|
@ -0,0 +1,70 @@ |
|
|
|
package com.jesuspinar.main; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.FilenameFilter; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
public class Main { |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
File folder = new File("."); |
|
|
|
StringBuilder sb = renderFolder(folder); |
|
|
|
System.out.println(sb.toString()); |
|
|
|
} |
|
|
|
|
|
|
|
private static StringBuilder renderFolder(File folder) { |
|
|
|
return renderFolder(folder, 0, new StringBuilder(), false, new ArrayList<>()); |
|
|
|
} |
|
|
|
|
|
|
|
private static StringBuilder renderFolder(File folder, int level, StringBuilder sb, |
|
|
|
boolean isLast, List<Boolean> hierarchyTree) { |
|
|
|
|
|
|
|
indent(sb, level, isLast, hierarchyTree).append(folder.getName()).append("\n"); |
|
|
|
|
|
|
|
File[] objects = folder.listFiles((FilenameFilter) new FilenameFilter() { |
|
|
|
@Override |
|
|
|
public boolean accept(File current, String name) { |
|
|
|
return new File(current, name).isDirectory(); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
for (int i = 0; i < objects.length; i++) { |
|
|
|
boolean last = ((i + 1) == objects.length); |
|
|
|
|
|
|
|
// esto significa que la carpeta actual todavía tendrá que imprimir sub |
|
|
|
//carpetas a este nivel, entonces necesitamos continuar imprimiendo | |
|
|
|
hierarchyTree.add(i != objects.length - 1); |
|
|
|
renderFolder(objects[i], level + 1, sb, last, hierarchyTree); |
|
|
|
|
|
|
|
// hace saltar el último valor al volver de un nivel inferior a un nivel superior |
|
|
|
hierarchyTree.remove(hierarchyTree.size() - 1); |
|
|
|
} |
|
|
|
return sb; |
|
|
|
} |
|
|
|
|
|
|
|
private static StringBuilder indent(StringBuilder sb, int level, |
|
|
|
boolean isLast, List<Boolean> hierarchyTree) { |
|
|
|
|
|
|
|
String indentContent = "\u2502 "; |
|
|
|
for (int i = 0; i < hierarchyTree.size() - 1; ++i) { |
|
|
|
// determina si necesitamos imprimir | en este nivel para mostrar la estructura del árbol |
|
|
|
// es decir, si esta carpeta tiene una carpeta hermana que se va a imprimir más tarde |
|
|
|
if (hierarchyTree.get(i)) { |
|
|
|
sb.append(indentContent); |
|
|
|
} else { |
|
|
|
sb.append(" "); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (level > 0) { |
|
|
|
sb.append(isLast |
|
|
|
? "\u2514\u2500\u2500" |
|
|
|
: "\u251c\u2500\u2500").append(" "); |
|
|
|
} |
|
|
|
|
|
|
|
return sb; |
|
|
|
} |
|
|
|
|
|
|
|
} |