Sonarqube 给我以下错误
使用 try-with-resources 或在“finally”子句中关闭此“Stream”
List<Path> paths = find(Paths.get(nasProps.getUpstreamOutputDirectory() + File.separator + inputSource.concat("_").concat(contentGroup).concat("_").concat(parentId)),
MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile() && filePath.getFileName().toString().matches(".*\\." + extTxt))
.collect(toList());
paths.stream().forEach(path -> textFileQueue.add(path));
我对java 8不太了解。你能帮我关闭流吗
假设find
这是Files.find,应该对你有用的是
final Path startPath = Paths.get(nasProps.getUpstreamOutputDirectory() +
File.separator +
inputSource.concat("_").concat(contentGroup).concat("_").concat(parentId));
BiPredicate<Path, BasicFileAttributes> matcher = (filePath, fileAttr) ->
fileAttr.isRegularFile() && filePath.getFileName().toString().matches(".*\\." + extTxt);
try (Stream<Path> pathStream = Files.find(startPath, Integer.MAX_VALUE, matcher)) {
pathStream.forEach(path -> textFileQueue.add(path));
} catch (IOException e) {
e.printStackTrace(); // handle or add to method calling this block
}
sonarqube在这里发出警告的原因在API note以及链接文档的:
此方法必须在 try-with-resources 语句中使用,或者
类似的控制结构以确保流的打开目录
流操作完成后立即关闭。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)