gpt4 book ai didi

java - Veracode CWE ID 404 资源关闭或释放不当

转载 作者:行者123 更新时间:2023-12-01 17:19:36 27 4
gpt4 key购买 nike

我有这个代码:

    try {
BufferedWriter bw = null;
FileWriter fw = null;
try {
final String fileName = getFileName();
File propertiesFile=new File(getFilesDir(), fileName);
fw = new FileWriter(propertiesFile);
bw = new BufferedWriter(fw);
bw.write(s);
File file = new File(getFilesDir(), fileName);

} catch (IOException e) {

}finally{
if(bw != null){
bw.close();
}
if(fw != null){
fw.close();
}
if(bw != null){
bw.close();
}
}
} catch (Exception e) {
}

Veracode 在此行检测到缺陷(不正确的资源关闭或释放)bw.write(s);如何解决这个问题?提前致谢

最佳答案

从 java 7 开始使用 try-with-resources

try (FileWriter fw = new FileWriter(propertiesFile);
BufferedWriter bw = new BufferedWriter(fw)){
......

}catch (Exception e){}

关于java - Veracode CWE ID 404 资源关闭或释放不当,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61320849/

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