我正在尝试将我的代码改编为Sonar,但我不知道为什么我无法纠正这个阻止程序。
使用此起始代码 所以,好的。我宣布将fileinputstream分开并继续,但它仍然不喜欢我关闭它的方式。 有什么想法吗?我正在关闭它,就像BufferedReader没有返回任何问题一样。BufferedReader localBufferedReader = null;
try {
localBufferedReader = new BufferedReader( new InputStreamReader( new FileInputStream(inputFile),"UTF-8")); //THIS INPUTSTREAM!!
String line;
//
// Fill Hashmap
HashMap hmVar = (HashMap) this.context.getAttribute(this.hmVarName);
if (hmVar == null)
hmVar = new HashMap();
String[] props = new String[2];
while ((line = localBufferedReader.readLine()) != null) {
//Split line into key, value
if(line.startsWith("#"))
continue;
props = line.split("=");
hmVar.put(props[0], props[1]);
}
this.context.setAttribute(this.hmVarName, hmVar);
} catch (FileNotFoundException localException2) {
this.context.logError("Unable to find file: " + inputFile);
localException2.printStackTrace();
throw new WFException("Unable to find file: " + inputFile);
} catch (Exception localException4) {
this.context.logError("Exception reading file: " + inputFile
+ " (" + localException4.getMessage() + ")");
localException4.printStackTrace();
throw new WFException("Exception reading file: " + inputFile
+ " (" + localException4.getMessage() + ")");
} finally {
try {
if (localBufferedReader != null) {
localBufferedReader.close();
}
} catch (Exception localException5) {
}
}
BufferedReader localBufferedReader = null;
FileInputStream fis = null;
try {
//StringBuffer localStringBuffer = new StringBuffer();
fis = new FileInputStream(inputFile);
//localBufferedReader = new BufferedReader( new InputStreamReader( new FileInputStream(inputFile),"UTF-8")); SONAR correction FileInputStream needs to be closed
localBufferedReader = new BufferedReader( new InputStreamReader( fis,"UTF-8"));
String line;
//
// Fill Hashmap
HashMap hmVar = (HashMap) this.context.getAttribute(this.hmVarName);
if (hmVar == null)
hmVar = new HashMap();
String[] props = new String[2];
while ((line = localBufferedReader.readLine()) != null) {
//Split line into key, value
if(line.startsWith("#"))
continue;
props = line.split("=");
hmVar.put(props[0], props[1]);
}
this.context.setAttribute(this.hmVarName, hmVar);
} catch (FileNotFoundException localException2) {
this.context.logError("Unable to find file: " + inputFile);
localException2.printStackTrace();
throw new WFException("Unable to find file: " + inputFile);
} catch (Exception localException4) {
this.context.logError("Exception reading file: " + inputFile
+ " (" + localException4.getMessage() + ")");
localException4.printStackTrace();
throw new WFException("Exception reading file: " + inputFile
+ " (" + localException4.getMessage() + ")");
} finally {
try {
if (localBufferedReader != null) {
localBufferedReader.close();
}
if (fis != null) {
fis.close();
}
} catch (Exception localException5) {
}
}
答案 0 :(得分:0)
Streams Readers和Writers实现了Closable接口。因此,如果您使用try-with-resource构造,则可以摆脱声纳警告:
try ( BufferedReader localBufferedReader = new BufferedReader( ...))
{
...
}
如果你离开try块,try(..)中任何已分配的Closable将自动关闭。