2016-09-28 86 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-與資源結構,你可以擺脫聲納警告。