2016-09-28 50 views
1

我想让我的代码适应声纳,我不明白为什么我不能纠正这个阻塞。关闭这个“FileInputStream”声纳

它是一种 '关闭此的FileInputStream' 这个起始码:

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) { 
       } 

      } 

所以,OK。我分开声明fileinputstream并继续,但它仍然不喜欢我关闭它的方式。

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) { 
       } 

      } 

任何想法?我正在以与返回没有问题的BufferedReader相同的方式关闭它。

+0

你可以尝试分离两个'close()'调用。如果从localbufferedReader.close()引发异常,你的'fis.close()'可能会被跳过。这可能是投诉的原因。 – rpy

回答

0

流读取器和写入器实现Closable接口。如果你离开try块

try (BufferedReader localBufferedReader = new BufferedReader(...)) 
    { 
     ... 
    } 

任何分配可关闭在try(..)将自动关闭:所以,如果你使用try-与资源结构,你可以摆脱声纳警告。