Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java “关闭此窗口”;FileInputStream";声纳_Java_Sonarqube - Fatal编程技术网

Java “关闭此窗口”;FileInputStream";声纳

Java “关闭此窗口”;FileInputStream";声纳,java,sonarqube,Java,Sonarqube,我正试图使我的代码适应声纳,我不明白为什么我不能纠正这个拦截器 这是一个“关闭此FileInputStream”,起始代码如下: BufferedReader localBufferedReader = null; try { localBufferedReader = new BufferedReader( new InputStreamReader( new FileInputStream(inputFile),"UTF-8")); /

我正试图使我的代码适应声纳,我不明白为什么我不能纠正这个拦截器

这是一个“关闭此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) {
                }

            }
所以,好吧。我声明分离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关闭它的方式相同,BufferedReader不会返回任何问题。

Streams Reader和Writer实现可关闭的接口。因此,如果使用try with resource结构,则可以消除声纳警告:

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

如果您离开try块,try(..)中分配的任何可关闭项都将自动关闭。

流读写器实现可关闭接口。因此,如果使用try with resource结构,则可以消除声纳警告:

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

如果您离开try块,try(..)中任何分配的可关闭项都将自动关闭。

您可以尝试分离两个
close()
调用。如果从
localBufferedReader.close()
引发异常,可能会跳过您的
fis.close()
。这可能是投诉的原因。您可以尝试将两个
close()
调用分开。如果从
localBufferedReader.close()
引发异常,可能会跳过您的
fis.close()
。这很可能是投诉的原因。