2016-09-28 17 views
-1

Sonar显示下面的代码是阻塞类型的错误,那么如何在finally块中关闭FileInputStream?这是抱怨我没有关闭FileInputStream但我该怎么做? FileInputStream是方法的返回类型,如果我在这里关闭,那么从它调用的地方就没有用处。请让我知道 - 如果同样的方法返回FileInputStream,如何关闭FileInputStreamJava-如果同样的方法返回'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(); 
    } 
} 
+0

之前申报与正确的格式文本形式的问题粘贴代码。 – progyammer

+0

@Antoniossss,他说。他不想关闭它,他表示疑惑为什么IDE抱怨。 –

回答

1

没有必要关闭输入相同的功能。问题可能是您不应在try{}区块中声明InputStream is以及放入return声明。

0

说就是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(); 
} 
}