2012-05-14 16 views
5

मैं रूट निर्देशिका में फ़ाइल ढूंढने की कोशिश कर रहा हूं और यह सब-निर्देशिका है।फ़ाइल खोज रिकर्सिवली

चरण 1- निर्दिष्ट पथ में निर्देशिका खोजें। चरण 2- यदि उपरोक्त निर्देशिका मिली है, तो इसकी एक उप-निर्देशिका में से किसी एक फ़ाइल को खोजें।

इसके लिए, मैं नीचे दिए गए कोड स्निपेट का उपयोग करता हूं जो दोबारा खोज करता है .. अब, यह मुद्दा यहां है, जब मैं अपनी उपर्युक्त आवश्यकताओं को पूरा करता हूं तो मैं रिकर्सन से कैसे टूट सकता हूं ..?

boolean bFileFound = false; 
File fileFound  = null; 

private void findFile(File aFile, String sDir){ 

    String filePath = aFile.getAbsolutePath(); 

    if(aFile.isFile() && filePath.contains(sDir)){ 

       if(aFile.getName().contains("test2.adv")){ 
        Log.d(TAG, "[FILE] " + aFile.getName()); 
        fileFound = aFile; 
        bFileFound = true; 
       } 

      // return true; 
    }else if(aFile.isDirectory()){ 

     String sDirName = aFile.getName(); 
     Log.d(TAG, "[DIR] " + sDirName); 

     if(sDirName.contains(sDir)){ 

      Log.d(TAG, "Found the directory..& Absolute Path = " + aFile.getAbsolutePath()); 
      sDir = sDirName; 
     } 

     File[] listFiles = aFile.listFiles(); 

     if(listFiles != null){ 

      for(int i = 0; i < listFiles.length; i++){ 

       if(bFileFound) 
        return; 

      findFile(listFiles[ i ], sDir); 
      } 
     }else{ 

      Log.d(TAG, " [ACCESS DENIED]"); 
     } 
    } 

    // return null; 
} 

धन्यवाद, डीके

उत्तर

6
/** 
* Search file a file in a directory. Please comment more here, your method is not that standard. 
* @param file the file/folder where to look our file for. 
* @param sDir a directory that must be in the path of the file to find 
* @param toFind the name of file we are looking for. 
* @return the file we were looking for. Null if no such file could be found. 
*/ 
private File findFile(File aFile, String sDir, String toFind){ 
    if(aFile.isFile() && 
      aFile.getAbsolutePath().contains(sDir) && 
      aFile.getName().contains(toFind)) { 
         return aFile; 
     } else if(aFile.isDirectory()) { 
     for(File child : aFile.listFiles()){ 
      File found = findFile(child, sDir, toFind); 
        if(found != null) { 
         return found; 
        }//if 
     }//for 
    }//else 
    return null; 
}//met 

अब, तीसरे परम जब आप findFile आह्वान के रूप में "test2.adv" गुजरती हैं। यह हार्डकोडिंग से ज्यादा दिलचस्प है।

यह भी ध्यान दें कि कई फाइलें आपकी खोज से मेल खाती हैं, यह फ़ंक्शन इसे अच्छी तरह से संभाल नहीं पाता है, यह पहले पाए गए वापस लौटाएगा।

+0

धन्यवाद Sincolas .. यह है awsome ... – codersnet

0

मैंने FileFilter का उपयोग करके इस समस्या को हल करने के लिए थोड़ा अलग दृष्टिकोण लिया और पुनरावर्ती खोज के लिए एक अलग विधि। मेरे मामले में ".json" एक्सटेंशन के साथ किसी भी फाइल की तलाश थी जहां फ़ाइल नाम का कोई फर्क नहीं पड़ता।

पहले, एक FileFilter कार्यान्वयन वर्ग फ़ाइल नाम धारण करने के लिए पुनरावर्ती खोज

/** 
* A {@link FileFilter} implementation that checks recursively files of a 
* specified fileName or extension string 
*/ 
public class FileExtensionFinder implements FileFilter { 
    private final String fileName; 
    private final List<File> foundFiles; 

    /** 
    * Constructor for FileExtensionFinder 
    * @param fileName string of the filename or extension being searched for 
    */ 
    public FileExtensionFinder(String fileName) { 
     this.fileName = fileName; 
     this.foundFiles = new ArrayList<>(); 
    } 

    @Override 
    public boolean accept(File pathname) { 
     // accept anything that is a folder or matches the fileName string 
     return pathname.isDirectory() || pathname.getName().toLowerCase().endsWith(fileName); 
    } 

    /** 
    * Searches recursively for all files with the provided filename/extension string 
    * @param filesArray an array of files (including folders) to iterate over 
    */ 
    public List<File> findFiles(File... filesArray) { 
     for (File file : filesArray) { 
      if (file.isDirectory()) { 
       findFiles(file.listFiles(this)); 
      } else if (file.getName().toLowerCase().endsWith(fileName)) { 
       foundFiles.add(file); 
      } 
     } 
     return foundFiles; 
    } 
} 

फिर बना सकते हैं और प्रदर्शन उपयोग काफी सरल है:

File fileLocation = // get your file here ... 
List<File> foundFiles = new FileExtensionFinder(".json").findFiles(fileLocation); 
संबंधित मुद्दे