Use try-with-resources or close this “BufferedOutputStream“ in a “finally“ clause.

Sonar常见问题及修改建议2021(持续更新!!)https://blog.csdn.net/libusi001/article/details/103717457

一、报错信息

Use try-with-resources or close this "BufferedOutputStream" in a "finally" clause.

使用try-with-resources或在“ finally”子句中关闭此“ BufferedOutputStream”。

二、源代码

try {
    BufferedOutputStream out = out = new BufferedOutputStream(
                        new FileOutputStream(new File(legalizeExamTemplatePath,newFileName)));
    out.write(file.getBytes());
    out.flush();
    out.close();
} catch (IOException e) {
    return Result.error("上传失败!");
            }

如果出现异常会跳转到catch,那么out.close()就不会执行。

所以需要在“ finally”子句中关闭此“ BufferedOutputStream”。

BufferedOutputStream需要写在try外面才行,而且必须初始化,初始化为null。

这样又会报空指针异常,所以需要判空,优化代码如下;

三、优化后代码

BufferedOutputStream out = null;
try {
    out = new BufferedOutputStream(
            new FileOutputStream(new File(legalizeExamTemplatePath, newFileName)));
    out.write(file.getBytes());
    out.flush();
    log.info("上传成功!");
    return Result.success("上传成功!", null);
} catch (IOException e) {
    return Result.error("上传失败!");
} finally {
    CloseIoUtils.closeAll(out);
}
try(BufferedOutputStream out = new BufferedOutputStream(
            new FileOutputStream(new File(legalizeExamTemplatePath, newFileName)));) {
    out.write(file.getBytes());
    out.flush();
    log.info("上传成功!");
    return Result.success("上传成功!", null);
} catch (IOException e) {
    return Result.error("上传失败!");
}

Java IO流关闭工具类详解:https://blog.csdn.net/libusi001/article/details/100741637

有用请点赞,养成良好习惯!

鼓励、疑问、交流请留言!

### 解决方案 为了确保 `FileOutputStream` 资源被正确关闭,可以采用两种主要方法:使用 try-with-resources 或者在 finally 子句中显式关闭流。 #### 方法一:Try-With-Resources Java 7 引入了 try-with-resources 语句来简化资源管理。这种方式自动处理资源的释放工作,在异常发生时也能保证资源会被关闭[^1]。 ```java // Using try-with-resources to handle resource automatically. public void writeToFile(String legalizeExamTemplatePath, String newFileName, byte[] fileBytes) { try (BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(new File(legalizeExamTemplatePath, newFileName)))) { out.write(fileBytes); out.flush(); } catch (IOException e) { System.out.println("上传失败!"); throw new RuntimeException(e); // 更好的做法是抛出自定义异常或者记录日志并返回错误信息给调用方 } } ``` 这种方法不仅使代码更加简洁易读,而且减少了因忘记手动关闭资源而引发的风险。 #### 方法二:Finally Clause 如果项目使用的 Java 版本低于 7,则可以在 try-catch 结构之外添加一个 finally 块用于关闭资源。即使发生了异常,finally 中的内容也会被执行[^2]。 ```java // Manually closing resources using finally block for versions before Java 7. public void writeToFileLegacy(String legalizeExamTemplatePath, String newFileName, byte[] fileBytes) { BufferedOutputStream out = null; try { out = new BufferedOutputStream( new FileOutputStream(new File(legalizeExamTemplatePath, newFileName))); out.write(fileBytes); out.flush(); } catch (IOException e) { System.out.println("上传失败!"); throw new RuntimeException(e); // 同样推荐更合适的异常处理方式 } finally { if (out != null) { try { out.close(); } catch (IOException ex) { // Handle exception when closing the stream } } } } ``` 无论选择哪种方法,都应确保所有打开的 I/O 流都能得到妥善关闭,从而避免潜在的安全漏洞和性能问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕白Lee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值