2016-09-28 6 views
1

私は自分のコードをSonarに適合させようとしています。なぜこのブロッカーを修正できないのか分かりません。この "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

2つの 'close()'呼び出しを分けることができます。 'localBufferedReader.close()'のような例外がスローされた場合、あなたの 'fis.close()'はスキップされるかもしれません。これはおそらく訴状の原因になります。 – rpy

答えて

0

ストリームリーダーとライターは、Closableインターフェイスを実装しています。あなたはtryブロックを残す場合は、自動的に閉じられます試してみる(..)で

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

どれ割り当てられた開閉可能:あなたは試し-とリソース構文を使用するのであれば、あなたはソナーの警告を取り除くことができます。

関連する問題