gpt4 book ai didi

java - SonarQube 未捕获未关闭的 FileInputStream

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:07:31 27 4
gpt4 key购买 nike

public static String getMD5Checksum(String filePath) throws Exception {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
return DigestUtils.md5Hex(fis);
}

在上面的代码中,fis 没有关闭,但在 SonarQube 中没有作为错误抛出。 DigestUtils.md5Hex 方法也不会关闭流。SonarQube 和 Sonar Java 插件都是最新版本。

最佳答案

您不使用 FileInputStream,但如果您添加以下行(或类似行):

fis.read();

它将添加有效规则:Resources should be closed (squid:S2095)

Connections, streams, files, and other classes that implement the Closeable interface or its super-interface, AutoCloseable, needs to be closed after use.

但是你有一点我们需要将资源释放为FileInputStream

Yes, you need to close the inputstream if you want your system resources released back.

您可以在SonarSource community中提出这个问题

编辑

Resources should be closed not warns on new FileInputStream添加为可能的 sonarqube 错误

关于java - SonarQube 未捕获未关闭的 FileInputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52519111/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com