1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
import java.io.File;
import java.io.FileOutputStream;
import javax.swing.*;
import javax.swing.tree.*;
import org.jdom.*;
import org.jdom.output.*;
public class Arbre extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
File fichier=new File("C:/Users/Alexandre/Documents/crypté_test"); //zone du fichier crypté
static Element racineXML = new Element("fichier crypté");
static org.jdom.Document document = new Document(racineXML);
public Arbre() {
// on définit le premier noeud
DefaultMutableTreeNode racine = new DefaultMutableTreeNode("fichier crypté", true);
DefaultMutableTreeNode cont = arborescence(fichier);
racine.add(cont);
affiche();
enregistre("test.xml");
}
public DefaultMutableTreeNode arborescence(File fic) {
Element dossier = new Element("dossier");
Element fich = new Element("fichier");
DefaultMutableTreeNode racine = new DefaultMutableTreeNode(fic,true); // création d'un noeud
File[] list = fic.listFiles(); // on recupère la liste des fichiers et sous rep
if ( list != null){
for (int j = 0 ; j<list.length ; j++) {
DefaultMutableTreeNode file = null;
if (list[j].isDirectory()) {
dossier.setText(list[j].getName());
racineXML.addContent(dossier);
}else{
fich.setText(list[j].getName());
racineXML.addContent(fich);
}
file = arborescence(list[j]);
racine.add(file);
}
}
return racine;
}
static void affiche()
{
try
{
//On utilise ici un affichage classique avec getPrettyFormat()
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
sortie.output(document, System.out);
}
catch (java.io.IOException e){}
}
static void enregistre(String fichier)
{
try
{
//On utilise ici un affichage classique avec getPrettyFormat()
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
//Remarquez qu'il suffit simplement de créer une instance de FileOutputStream
//avec en argument le nom du fichier pour effectuer la sérialisation.
sortie.output(document, new FileOutputStream(fichier));
}
catch (java.io.IOException e){}
}
} |
Partager