2016-07-20 5 views
1

常にそれが「level.dat」と呼ばれる特定のファイルのディレクトリを検索し、以下のこのメソッドはnullを返します:ディレクトリ検索メソッドは常にnullを返します

File leveldat = traverse(wftemp,"level.dat"); 

private static File traverse(File dir, String name) { 
    if (dir.isDirectory()) { 
     String[] children = dir.list(); 
     for (int i = 0; children != null && i < children.length; i++) { 
      traverse(new File(dir, children[i]),name.trim()); 
     } 
    } 
    if (dir.isFile()) { 
     if (dir.getName().trim().equalsIgnoreCase(name.trim())) { 
      return dir; 
     } 
    } 
return null; 
} 

は、メソッドは、この使用して呼び出されます

wftempはディレクトリへのパスです。

いくつかのprintlnを置くことによって、実際にファイルが見つかることがわかりました。それを正しく返すことはありません。 プリントラインコード:

-snip- 
C:\Users\mrjvs\AppData\Roaming\the stanley parable\temp\stanleyparablemoesh\The stanley parable for moesh\DIM1\##MCEDIT.TEMP## 
C:\Users\mrjvs\AppData\Roaming\the stanley parable\temp\stanleyparablemoesh\The stanley parable for moesh\DIM1\##MCEDIT.TEMP2## 
C:\Users\mrjvs\AppData\Roaming\the stanley parable\temp\stanleyparablemoesh\The stanley parable for moesh\DIM1\playerdata 
C:\Users\mrjvs\AppData\Roaming\the stanley parable\temp\stanleyparablemoesh\The stanley parable for moesh\icon.png 
C:\Users\mrjvs\AppData\Roaming\the stanley parable\temp\stanleyparablemoesh\The stanley parable for moesh\level.dat 
We found a match: C:\Users\mrjvs\AppData\Roaming\the stanley parable\temp\stanleyparablemoesh\The stanley parable for moesh\level.dat 
C:\Users\mrjvs\AppData\Roaming\the stanley parable\temp\stanleyparablemoesh\The stanley parable for moesh\level.dat_old 
C:\Users\mrjvs\AppData\Roaming\the stanley parable\temp\stanleyparablemoesh\The stanley parable for moesh\mcedit_waypoints.dat 
C:\Users\mrjvs\AppData\Roaming\the stanley parable\temp\stanleyparablemoesh\The stanley parable for moesh\playerdata 
C:\Users\mrjvs\AppData\Roaming\the stanley parable\temp\stanleyparablemoesh\The stanley parable for moesh\playerdata\4e879a3b-c247-4d9a-8ec8-577172a00356.dat 
C:\Users\mrjvs\AppData\Roaming\the stanley parable\temp\stanleyparablemoesh\The stanley parable for moesh\region 
C:\Users\mrjvs\AppData\Roaming\the stanley parable\temp\stanleyparablemoesh\The stanley parable for moesh\region\r.-1.-1.mca 
-snip- 

私は1.5時間以上しようとしている:

private static File traverse(File dir, String name) { 
    System.out.println(dir.getAbsolutePath()); 
    if (dir.isDirectory()) { 
     String[] children = dir.list(); 
     for (int i = 0; children != null && i < children.length; i++) { 
      traverse(new File(dir, children[i]),name.trim()); 
     } 
    } 
    if (dir.isFile()) { 
     if (ddir.getName().trim().equalsIgnoreCase(name.trim())) { 
      System.out.println("We found a match: " + dir.getAbsolutePath()); 
      return dir; 
     } 
    } 
return null; 
} 

これは、コンソールがプリントラインで返すものです。私はもう知らない。

+0

あなたの主な問題は再帰性です。あなたは何を返そうとしていますか?ファイルのパス?またはそれを含むディレクトリ? –

+0

ファイル自体を返すようにします。それはいくつかのフォルダに埋もれています。私はそれを見つけるためにこれを使います。 – mrjvs

答えて

0

実際にはあまり変わったことではありませんが、あなたは何か再帰的なことをしようとしています。しかし、traverseへの再帰呼び出しは使用されません。今のように、あなたが見つけたいと思う道でそれを呼び出すと、それはうまくいくでしょう。このように、横断するコールへの復帰を追加してみてください:

for (int i = 0; children != null && i < children.length; i++) { 
    File result = traverse(new File(dir, children[i]),name.trim()); 
    if(result != null) { 
     return result; 
    } 
} 
+0

これは機能しませんでした。それは、検索されたファイルを5に減らしました。ファイルには、私が探しているファイルが含まれていませんでした。 – mrjvs

+0

ああ、今のところ、私はトラバースから返された値をチェックしていませんでした。 –

+0

が動作します。ありがとうございます – mrjvs

1
private static File traverse(File dir, String name) { 
    if (dir.isDirectory()) { 
     String[] children = dir.list(); 
     for (int i = 0; children != null && i < children.length; i++) { 
      File tmp = traverse(new File(dir, children[i]),name.trim()); 
      if(tmp != null) { 
       return tmp; 
      } 
     } 
    } 
    if (dir.isFile()) { 
     if (dir.getName().trim().equalsIgnoreCase(name.trim())) { 
      return dir; 
     } 
    } 
    return null; 
} 

が複数の一致の場合には、これが唯一の最初の返されます。 さようなら。

+0

これで解決しました。ありがとうございました。 – mrjvs

関連する問題