-1

Sonar正在显示以下代码的错误,该错误属于阻止程序类型。它抱怨我没有关闭FileInputStream,但我该怎么做?FileInputStream是方法的返回类型,如果我在此处关闭,那么它从调用的地方将毫无用处。请让我知道 - 如果相同的方法返回,我该如何关闭FileInputStreamfinally 块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();
    }
}
4

2 回答 2

2

无需关闭同一函数中的输入。问题可能是您不应该InputStream istry{}块中声明以及放置return语句。

于 2016-09-28T10:19:07.550 回答
0

Put 是 try 块之前的声明

 InputStream is= null;
try {
    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();
}finally{
  if(is !=null){
  is.close();
 }
}
于 2016-09-28T10:21:58.453 回答