Files
BUT2/TP_DEV3.2/Arbres/Node.java

51 lines
1.1 KiB
Java
Raw Normal View History

2025-11-27 13:53:52 +01:00
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("");
}
}