package projet; import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.NoSuchElementException; /** * Cette classe sert a trier les fichiers par rapport a leur nom. * @author Bui Christophe & Mahé Julien */ public class ArboTrieeParNom { private List liste = new ArrayList(); private FileProperty p1; public ArboTrieeParNom(File file) { parcourir(file); Collections.sort(liste); } public File rechercherFichier(String nom) throws NoSuchElementException{ for (File file : liste) { if (file.getName().equals(nom)) { return file; } } throw new NoSuchElementException("Il n'y a pas d'abonné ayant le nom " + nom); } public void parcourir(File file) { if (file.isDirectory()) { File [] list = file.listFiles(); if (list != null) { for (int i = 0; i < list.length; i++) { parcourir(list[i]); } } } else { if (file.getName().endsWith("pdf")) { liste.add(file); } } } public List getListe() { return liste; } public void setListe(List liste) { this.liste = liste; } /** * * Cette méthode permet de sérialiser les informations d'un fichier. * @param le nom du fichier filename. * */ public void serialize(String fileName) { ObjectOutputStream oos; try { oos = new ObjectOutputStream(new FileOutputStream(fileName)); oos.writeObject(getListe()); oos.close(); } catch (FileNotFoundException e) { System.err.println(e.getMessage()); } catch (IOException e) { System.err.println(e.getMessage()); } } /** * * Cette méthode permet de lire les informations d'un fichier déjà sérialisé. * @param le nom du fichier filename. * */ public void read(String fileName) { try { ObjectInputStream inputStream = new ObjectInputStream( new FileInputStream(fileName)); List readObject = (List) inputStream.readObject(); setListe(readObject); inputStream.close(); } catch (FileNotFoundException e) { System.err.println(e.getMessage()); } catch (ClassNotFoundException e) { System.err.println(e.getMessage()); } catch (IOException e) { System.err.println(e.getMessage()); } } /** * * Cette méthode permet d'afficher les informations d'un fichier . * @return les informations du fichier * */ public String toString() { String s = "Les fichiers pdf suivants sont présents dans l'arborescence : \n"; for (File fichier : liste) { p1 = new FileProperty(fichier.getName()); s = s + " Nom : " + fichier + " Date : " + p1.getFormatedDate() + " Taille : " + p1.getFormatedSize() + " Type : " + p1.getType() + " Extension : " + p1.getFileExt(fichier.getName()) + "\n"; } return s; } }