Advertisement
Guest User

Fix FileSearch

a guest
Jul 30th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1.  
  2. import java.io.File;
  3. import java.io.IOException;
  4. import android.os.Environment;
  5. import java.io.FileNotFoundException;
  6.  
  7. /**
  8.  * Class utilitaire pour chercher un fichier avec son nom.
  9.  * @author Laurent  
  10.  */
  11. public final class FileSearch {
  12.  
  13.     private FileSearch() {
  14.     }
  15.    
  16.     /**
  17.      *   * Methode qui va rechercher dans la sdcard le fichier avec le nom.   *
  18.      *  
  19.      * @param fileName le nom du fichier  
  20.      * @return le fichier en lui meme  
  21.      */
  22.     public static File chercherFichier(final String fileName) throws FileNotFoundException {
  23.         final File root = Environment.getExternalStorageDirectory();
  24.         final File file = chercherFichierRec(root, fileName);
  25.         if( file == null) {
  26.             throw new FileNotFoundException("File "+fileName+" not found in external storage.");
  27.         }
  28.         return file;
  29.     }
  30.  
  31.     private static File chercherFichierRec(final File dossier, final String fileName) {
  32.         File out = null;
  33.         final File[] contenuDossier = dossier.listFiles();
  34.         for (final File f : contenuDossier) {
  35.             if (f.isDirectory()) {
  36.                 out = chercherFichierRec(f, fileName);
  37.                 if (out != null) {
  38.                     break;
  39.                 }
  40.             } else if (f.isFile() && f.getName().equals(fileName)) {
  41.                 out = f;
  42.                 break;
  43.             }
  44.         }
  45.         return out;
  46.     }
  47.  
  48.     /**
  49.      *   * cherche le dossier par le nom   *  
  50.      *
  51.      * @param fileName  
  52.      * @return  
  53.      */
  54.     public static File chercherDossier(final String fileName) throws FileNotFoundException {
  55.         final File root = Environment.getExternalStorageDirectory();
  56.         final File file = chercherFichierRec(root, fileName);
  57.         if( file == null) {
  58.             throw new FileNotFoundException("File "+fileName+" not found in external storage.");
  59.         }
  60.         return file;
  61.     }
  62.  
  63.     private static File chercherDossierRec(final File dossier, final String fileName) {
  64.         File out = null;
  65.         final File[] contenuDossier = dossier.listFiles();
  66.         for (final File f : contenuDossier) {
  67.             if (f.isDirectory() ) {
  68.                 if (f.getName().equals(fileName)) {
  69.                     out = f;
  70.                     break;
  71.                 } else {
  72.                     out = chercherFichierRec(f, fileName);
  73.                     if (out != null) {
  74.                         break;
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.         return out;
  80.     }
  81.  
  82.     /**
  83.      *   * supprime tous les fichiers et dossier dans ce dossier.   * supprime
  84.      * le dossier en tant que tel, si true.  
  85.      *
  86.      * @param root  
  87.      */
  88.     public static boolean deleteAllFileFolder(final File root, final boolean delete) {
  89.         if (root.isDirectory()) {
  90.             for (File f : root.listFiles()) {
  91.                 deleteAllFileFolder(f, true);
  92.             }
  93.         }
  94.         if (delete) {
  95.              return root.delete();
  96.         }
  97.         return true;
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement