|
|
@ -0,0 +1,74 @@ |
|
|
|
package listarDirectorioTree; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.util.Iterator; |
|
|
|
|
|
|
|
public class tree { |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
|
|
|
|
String path = "/home/michael/Downloads"; |
|
|
|
File f = new File(path); |
|
|
|
|
|
|
|
System.out.println(printDirectoryTree(f)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//Print Tree |
|
|
|
public static String printDirectoryTree(File directorio) { |
|
|
|
if(!directorio.isDirectory()) { |
|
|
|
throw new IllegalArgumentException("No es un directorio"); |
|
|
|
} |
|
|
|
|
|
|
|
int indent = 0; |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
printDirectoryTree(directorio, indent, sb); |
|
|
|
return sb.toString(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// |
|
|
|
|
|
|
|
private static void printDirectoryTree(File directorio, int indent, StringBuilder sb) { |
|
|
|
|
|
|
|
if(!directorio.isDirectory()) { |
|
|
|
throw new IllegalArgumentException("No es un directorio"); |
|
|
|
} |
|
|
|
|
|
|
|
sb.append(getIndentString(indent));sb.append("+--");sb.append(directorio.getName());sb.append("/");sb.append("\n"); |
|
|
|
|
|
|
|
for (File fichero : directorio.listFiles()) { |
|
|
|
if(fichero.isDirectory()) { |
|
|
|
printDirectoryTree(fichero, indent + 1, sb); |
|
|
|
}else { |
|
|
|
printFichero(fichero,indent + 1 , sb); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Por un subdirectorio o fichero que no haya ponemos un espacio (indent) |
|
|
|
// Por un subdirectorio o fichero ponermos un +-- |
|
|
|
// Al final damos un espacio con /n |
|
|
|
|
|
|
|
private static void printFichero(File fichero,int indent, StringBuilder sb) { |
|
|
|
|
|
|
|
sb.append(getIndentString(indent));sb.append("+--");sb.append(fichero.getName());sb.append("\n"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//Dar espacio |
|
|
|
private static String getIndentString(int indent ) { |
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
|
|
|
for (int i = 0; i < indent; i++) { |
|
|
|
sb.append("| "); |
|
|
|
} |
|
|
|
return sb.toString(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |