我正在尝试编写一种列出目录中所有非隐藏文件的方法。但是,当我添加条件时!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();
}
传递给的 lambda 表达式Iterable#forEach http://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html#forEach-java.util.function.Consumer-不允许抛出异常,所以你需要在那里处理它:
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
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)