有人可以幫我解決我的問題。我有一個函數,用於檢查文件是否存在於特定路徑中。該函數檢查文件名是否匹配,路徑是否匹配(具有特定名稱的文件可能存在於多個位置)。請在下面找到我的代碼。退貨聲明不起作用
memberPath是一個包含相對路徑的靜態變量。 file_Path是一個靜態變量,當找到匹配項時它會被更新。
我的問題是該函數找到匹配,但它突然出現for循環返回語句,但回到for循環。有人可以幫助我修復我的代碼,以便一旦找到匹配,它將返回到呼叫位置。
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;
}
這就是遞歸。你期望會發生什麼? – 2013-02-19 08:34:14
爲什麼不,而不是立即返回? – 2013-02-19 08:36:35
@RohitJain:但是你能告訴我應該添加什麼,以便它返回到呼叫位置 – user1688404 2013-02-19 08:36:50