package projet; import java.io.*; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.NoSuchElementException; import javax.swing.JFileChooser; public class ArboTrieeParNom implements ArboTriee { private List liste = new ArrayList(); 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 de fichier 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; } 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()); } System.out.println("good"); } 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()); } } public String getFormatedSize(File file) { int size = (int) (file.length() / 1024) + 1; if (size > 1024) { return (size / 1024) + " Mo"; } else { return size + " ko"; } } public static String getFileExt(String filename) { int pos = filename.lastIndexOf("."); if (pos > -1) { return filename.substring(pos); } else { return filename; } } public String getType(File file) { JFileChooser chooser = new JFileChooser(); return chooser.getTypeDescription(file); } public String toString() { String s = "Les fichiers pdf suivants sont présents dans l'arborescence : \n"; for (File fichier : liste) { SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy H:mm:ss"); Date d = new Date(fichier.lastModified()); s = s + " Nom : " + fichier + " Date : " + sdf.format(d) + " Taille : " + getFormatedSize(fichier) + " Type : " + getType(fichier) + " Extension : " + getFileExt(fichier.getName()) + "\n"; } return s; } }