java - 目录搜索方法总是返回 null

标签 java

下面的这个方法总是在目录中搜索名为“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/

相关文章:

java - 使用 Java SDK 的带有服务凭证的 Google Gmail API 请求返回 400

java - Primefaces - 带有 CommandLink 的上下文菜单

java - 在 Android 上进行 SHA1 加密?

Java 字节码可视化工具

java - Travis CI 找不到 Ivy jarfile

java - 如何将表从一个数据库复制到另一个数据库?

java - 程序的大 O 表示法(最坏情况)

java - Karate 框架 - 如何检查比赛中的条件 OR 包含响应

java - 使用链表的堆栈回文不会返回正确答案

java - 消费者 key 或访问 token 是否会更改 Twitter 密码?