51 lines
1.1 KiB
Java
51 lines
1.1 KiB
Java
import java.io.File;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
|
|
public class Node {
|
|
|
|
String name;
|
|
List<Node> children = new ArrayList<>();
|
|
|
|
public Node(String name){
|
|
this.name = name;
|
|
}
|
|
|
|
public void addChild(Node child){
|
|
children.add(child);
|
|
}
|
|
|
|
public void afficher(String indent){
|
|
System.out.println(indent + name);
|
|
for(Node c : children){
|
|
c.afficher(indent + " "); // indentation augmentée
|
|
}
|
|
}
|
|
|
|
public static Node build(File f){
|
|
Node n = new Node(f.getName());
|
|
File[] list = f.listFiles();
|
|
|
|
if(list != null){
|
|
for(File child : list){
|
|
n.addChild(build(child)); // récursion OK
|
|
}
|
|
}
|
|
|
|
return n;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
if(args.length != 1){
|
|
System.out.println("Usage : java Node <repertoire>");
|
|
return; // éviter un crash
|
|
}
|
|
|
|
File fichier = new File(args[0]);
|
|
Node tree = build(fichier);
|
|
tree.afficher("");
|
|
}
|
|
}
|