42 lines
1.4 KiB
Java
42 lines
1.4 KiB
Java
|
|
import java.io.File;
|
||
|
|
|
||
|
|
// Classe représentant un nœud de l'arbre
|
||
|
|
public class Noeud {
|
||
|
|
String nom;
|
||
|
|
Noeud[] enfants;
|
||
|
|
|
||
|
|
public Noeud(String nom) {
|
||
|
|
this.nom = nom;
|
||
|
|
this.enfants = new Noeud[0]; // Initialement pas d'enfants
|
||
|
|
}
|
||
|
|
|
||
|
|
public void setEnfants(Noeud[] enfants) {
|
||
|
|
this.enfants = enfants;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void afficher(String prefixe) {
|
||
|
|
System.out.println(prefixe + nom);
|
||
|
|
for (Noeud enfant : enfants) {
|
||
|
|
enfant.afficher(prefixe + " ");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Méthode pour construire un arbre à partir d'un répertoire
|
||
|
|
public static Noeud construireArbre(File fichier) {
|
||
|
|
Noeud noeud = new Noeud(fichier.getName());
|
||
|
|
if (fichier.isDirectory()) {
|
||
|
|
File[] contenu = fichier.listFiles();
|
||
|
|
if (contenu != null) {
|
||
|
|
Noeud[] enfants = new Noeud[contenu.length];
|
||
|
|
for (int i = 0; i < contenu.length; i++) {
|
||
|
|
System.out.print(fichier.getName()+":");
|
||
|
|
System.out.print(contenu[i]);
|
||
|
|
System.out.println("");
|
||
|
|
enfants[i] = construireArbre(contenu[i]);
|
||
|
|
}
|
||
|
|
noeud.setEnfants(enfants);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return noeud;
|
||
|
|
}
|
||
|
|
}
|