下面的这个方法总是在目录中搜索名为“level.dat”的特定文件返回 null:
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;
}
使用以下方法调用该方法:
File leveldat = traverse(wftemp,"level.dat");
wftemp 是目录的路径。
通过放置几个 println,我发现它确实找到了该文件。但没有正确返回。 打印行代码:
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;
}
这是控制台通过打印行返回的内容:
-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) {
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;
}
如果有多个匹配项,则仅返回第一个匹配项。 再见。
关于java - 目录搜索方法总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38483424/