2016-07-20 4 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을 배치하여 실제로 파일을 찾았습니다. 그러나 그것을 올바르게 되돌려주지 않습니다. printline 코드 :

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

이는 printlines와 콘솔 반환 것입니다. 나는 이제 더 이상 모른다.

+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

관련 문제