2016-11-15 5 views
-1

私には擬似コードを与えられましたが、私はそれをすべて理解できません。拡張子は変数「ext」に割り当てられます特定の内線番号を持つすべてのファイルを再帰的に表示する

If f.isFile() is true, then 
If f.getPath() ends with the extension, then 
    Add f.getPath() to the foundFiles array list 
Return // this is the end of recursion 
Else // This must be a directory 
For each subFile in f.listFiles() // This gets all the files in the directory 
    Call findMatchingFiles(subFile) // This is the recursive call 

これはこれまでのところ空白を埋めるように見えません。任意のヒントや助けが大歓迎です。

public void findMatchingFiles(File f) { 

    if (f.isFile() == true) { 
     if() { 

     foundFiles.add(f.getPath()); 
     } 

     return; 
    } else { 
     for (:) { 
      findMatchingFiles(subFile); 
     } 

    } 

} 
} 

答えて

0
public void findMatchingFiles(File f) { 

    //i added this. you need to change it to be whatever extension you want to match 
    String myExtension = ".exe"; 

    if (f.isFile() == true) { 

     //i added this block. it gets the extension and checks if it matches 
     int i = fileName.lastIndexOf('.'); 
     String extension = fileName.substring(i+1); 
     if (extension.equals(myExtension)) { 
      foundFiles.add(f.getPath()); 
     } 
     return; 
    } else { 

     //i added this. it gets all the files in a folder 
     for (File subFile : f.listFiles()) { 
      findMatchingFiles(subFile); 
     } 
    } 
} 

上記のコードは、あなたの問題を解決する必要があります。あなたが欠けていた2つのものは次のとおりでした:

  1. フォルダ内のファイルを取得する方法。 Google検索でこれが見つかりました:Getting the filenames of all files in a folder
  2. ファイル拡張子を取得する方法。 Google検索でこれが見つかりました:How do I get the file extension of a file in Java?

私はこれらのコードを両方ともあなたのコードに差し込みました。正常に動作するはずです。また、追加した変数にはmyExtensionという名前が付けられています。この変数を変更して、実際に一致させたい拡張子を反映させる必要があります。

関連する問題