import java.io.File;
public class MovieFinder {
public static void main(String[] args) {
String folderPath = "/path/to/folder"; // 将文件夹路径替换为你要搜索的真实路径
boolean found = searchFolder(folderPath);
if (found) {
System.out.println("找到以avi结尾的电影");
} else {
System.out.println("未找到以avi结尾的电影");
}
}
public static boolean searchFolder(String folderPath) {
File folder = new File(folderPath);
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
boolean found = searchFolder(file.getAbsolutePath());
if (found) {
return true;
}
} else {
if (file.getName().endsWith(".avi")) {
return true;
}
}
}
}
return false;
}
}
在上面的示例中,首先指定要搜索的文件夹路径 folderPath
,然后调用 searchFolder
方法来执行递归搜索。如果找到以 “.avi” 结尾的文件,方法会立即返回 true
,表示找到了电影文件。如果搜索完整个文件夹仍然没有找到匹配的文件,方法会返回 false
。你可以根据需要修改代码来适应你的具体场景。记得将 "/path/to/folder"
替换为你要搜索的真实文件夹路径。