下面的方法总是在目录中搜索名为" level.dat"的特定文件。返回null:
-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}}
wftemp是目录的路径。
通过放置几个println,我发现它确实找到了该文件。但是没有正确回归。 印刷品代码:
{{1}}
这是控制台返回的打印行:
{{1}}
我已经尝试了超过1.5小时。我不知道了。
答案 0 :(得分: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;
}
如果有多个匹配,则仅返回第一个匹配。 再见。
答案 1 :(得分:0)
traverse
的递归调用。现在它只有在您想要查找的路径上调用它时才会起作用。尝试添加对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;
}
}