2

When I ananyze some code using sonarlint in eclipse which close the FileReader in the finally block, the sonarlint prompt me that "Close this 'FileReader'" which is generated by the rule "Resources should be closed". Is this a bug from SonarLint?. Is this a bug from SonarLint?

I can't use try-with-resources features, because our project using JDK 1.6

The code is like below:

FileReader fr = null;
try {
    fr = new FileReader(pidFile);
    oldPid = new BufferedReader(fr).readLine();
}
catch (IOException e) {
    LOGGER.error(e.getMessage(), e);
}
finally {
    try {
        if (fr != null) {
            fr.close();
        }
    }
    catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }
}
Linhoo
  • 33
  • 7

1 Answers1

0

You are not closing the buffered reader in the finally block. I suggest you remove everything in the finally block and add the following
IOUtils.closeQuietly(fr); IOUtils.closeQuietly(oldPid);