2012-05-14 32 views
5

私は、ルートディレクトリとそれがサブディレクトリにあるファイルを検索しようとしています。ファイルの再帰的検索

は、指定されたパスにディレクトリを検索しますStep1-。 ステップ2 - 上記のディレクトリが見つかった場合は、そのサブディレクトリの1つでファイルを探します。このため

、私は再帰的に検索し、以下のコードスニペットを使用して...さて、ここでの問題は、それが私の上記の要件の両方を満たしたとき、私は再帰から抜け出すないか、ですか..?

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; 
} 

おかげで、 DK今

答えて

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 

は、あなたがファイル探索を起動する第三のparamとして "test2.adv" を渡します。それはハードコーディングより面白いです。

はまた、それは最初に見つかったものを返しますが、この関数はそれをうまく処理できない、つまり複数のファイルが検索条件に一致可能性がありますのでご注意ください。

+0

おかげSincolas ...それはですawsome ... – codersnet

0

私はFileFilterと再帰的に検索するための別の方法を使用してこの問題を解決するために、わずかに異なるアプローチを取りました。私の場合は、ファイル名の大文字小文字の区別がない「.json」拡張子を持つファイルを探していました。

まず、次に、ファイル名を保持し、

/** 
* 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; 
    } 
} 

再帰検索を実行するためにFileFilter実装クラスを作成し、使用量が非常に簡単です:

File fileLocation = // get your file here ... 
List<File> foundFiles = new FileExtensionFinder(".json").findFiles(fileLocation); 
関連する問題