以下不断报告相同的错误。
public static ArrayList<File> findMatches(File directory, String pattern) throws FileNotFoundException {
60 ArrayList<File> container = new ArrayList<File>();
61
62 return container;
63 }
我用以下方式初始化它
ArrayList<File> matched = findMatches(new File("hw9"), "FileFinder.java");
错误:错误:未报告的异常 FileNotFoundException; 必须被抓住或宣布被扔掉
有什么解决办法吗?
编辑
终于知道怎么做了!
public static ArrayList<File> findMatches(File directory, String pattern) throws FileNotFoundException {
ArrayList<File> container = new ArrayList<File>();
try {
if (!directory.exists() && !directory.canRead() && !directory.isDirectory()) {
throw new FileNotFoundException();
}
File[] fileStack = directory.listFiles();
for (int i = 0; i < fileStack.length; i++) {
if (patternMatches(pattern, fileStack[i].getName())) {
container.add(fileStack[i]);
}
}
} catch (NullPointerException e) {
throw new FileNotFoundException();
}
return container;
}