Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import android.os.Environment;
- import java.io.FileNotFoundException;
- /**
- * Class utilitaire pour chercher un fichier avec son nom.
- * @author Laurent
- */
- public final class FileSearch {
- private FileSearch() {
- }
- /**
- * * Methode qui va rechercher dans la sdcard le fichier avec le nom. *
- *
- * @param fileName le nom du fichier
- * @return le fichier en lui meme
- */
- public static File chercherFichier(final String fileName) throws FileNotFoundException {
- final File root = Environment.getExternalStorageDirectory();
- final File file = chercherFichierRec(root, fileName);
- if( file == null) {
- throw new FileNotFoundException("File "+fileName+" not found in external storage.");
- }
- return file;
- }
- private static File chercherFichierRec(final File dossier, final String fileName) {
- File out = null;
- final File[] contenuDossier = dossier.listFiles();
- for (final File f : contenuDossier) {
- if (f.isDirectory()) {
- out = chercherFichierRec(f, fileName);
- if (out != null) {
- break;
- }
- } else if (f.isFile() && f.getName().equals(fileName)) {
- out = f;
- break;
- }
- }
- return out;
- }
- /**
- * * cherche le dossier par le nom *
- *
- * @param fileName
- * @return
- */
- public static File chercherDossier(final String fileName) throws FileNotFoundException {
- final File root = Environment.getExternalStorageDirectory();
- final File file = chercherFichierRec(root, fileName);
- if( file == null) {
- throw new FileNotFoundException("File "+fileName+" not found in external storage.");
- }
- return file;
- }
- private static File chercherDossierRec(final File dossier, final String fileName) {
- File out = null;
- final File[] contenuDossier = dossier.listFiles();
- for (final File f : contenuDossier) {
- if (f.isDirectory() ) {
- if (f.getName().equals(fileName)) {
- out = f;
- break;
- } else {
- out = chercherFichierRec(f, fileName);
- if (out != null) {
- break;
- }
- }
- }
- }
- return out;
- }
- /**
- * * supprime tous les fichiers et dossier dans ce dossier. * supprime
- * le dossier en tant que tel, si true.
- *
- * @param root
- */
- public static boolean deleteAllFileFolder(final File root, final boolean delete) {
- if (root.isDirectory()) {
- for (File f : root.listFiles()) {
- deleteAllFileFolder(f, true);
- }
- }
- if (delete) {
- return root.delete();
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement