退货声明不起作用

use*_*404 2 java

有人可以帮我修复我的问题.我有一个函数,它检查特定路径中是​​否存在文件.该函数检查文件名是否匹配,路径是否也匹配.(具有特定名称的文件可能存在于多个位置).请在下面找到我的代码.

memberPath是一个包含相对路径的静态变量.file_Path是一个静态变量,在找到匹配项后会更新.

我的问题是函数找到了匹配,但它突然出现for循环返回语句但返回到for循环.有人可以帮我修改我的代码,这样一旦找到匹配,它就会将bac返回到调用位置.

public static String traverse(String path, String filename) {
        String filePath = null;
        File root = new File(path);
        File[] list = root.listFiles();

        for (File f : list) {
            if (f.isDirectory()) {
                traverse(f.getAbsolutePath(), filename);
            } else if (f.getName().equalsIgnoreCase(filename) && f.getAbsolutePath().endsWith(memberPath)) {
                filePath = f.getAbsolutePath();
                file_Path = filePath;
                break ;
                }
        }
        return filePath;
}
Run Code Online (Sandbox Code Playgroud)

Bob*_*der 6

添加:

return traverse(f.getAbsolutePath(), filename);
Run Code Online (Sandbox Code Playgroud)

返回此调用获得的值.

正如所指出的 - 您可以返回值而不是break.