我正在尝试编写一种列出目录中所有非隐藏文件的方法。但是,当我添加条件时,!Files.isHidden(filePath)
我的代码将无法编译,并且编译器返回以下错误:
java.lang.RuntimeException: Uncompilable source code - unreported exception
java.io.IOException; must be caught or declared to be thrown
我试图抓住IOException
,但是编译器仍然拒绝编译我的代码。有什么明显的我想念的东西吗?代码在下面列出。
try {
Files.walk(Paths.get(root)).forEach(filePath -> {
if (Files.isRegularFile(filePath) && !Files.isHidden(filePath)) {
System.out.println(filePath);
} });
} catch(IOException ex) {
ex.printStackTrace();
} catch(Exception ex) {
ex.printStackTrace();
}
Iterable#forEach
不允许传递给lambda表达式引发异常,因此您需要在那里处理它:
Files.walk(Paths.get(root)).forEach(filePath -> {
try {
if (Files.isRegularFile(filePath) && !Files.isHidden(filePath)) {
System.out.println(filePath);
}
} catch (IOException e) {
e.printStackTrace(); // Or something more intelligent
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句