【发布时间】:2016-09-28 10:13:07
【问题描述】:
Sonar 正在显示以下代码的错误,该错误属于拦截器类型。它抱怨我没有关闭FileInputStream,但我该怎么做呢? FileInputStream 是方法的返回类型,如果我在此处关闭,那么它从调用的地方将毫无用处。请让我知道 - 如果相同的方法返回 FileInputStream,我如何在 finally 块中关闭 FileInputStream?
代码如下:
@Override
public InputStream getInputStream() throws MissingObjectException {
try {
InputStream is;
if (smbFile != null) {
is = new BufferedInputStream(new SmbFileInputStream(smbFile), 60000);
}
else {
is = new BufferedInputStream(new FileInputStream(getFilePath()));
}
return is;
}
catch (Exception e) {
throw new MissingObjectException();
}
}
【问题讨论】:
-
以文本形式将代码粘贴到问题中,格式正确。
-
@Antoniossss 他这么说。他不想关闭它,他表示想知道 IDE 为什么会抱怨。
标签: java sonarqube fileinputstream autocloseable