Lol
Lol

Reputation: 87

Use try-with-resources or close this "BufferedReader" in a "finally" clause

Been looking for a way to fix this issue. Read all the previous answers but none helped me out. Could it be any error with SonarQube?

public class Br {

    public String loader(String FilePath){

        BufferedReader br;
        String str = null;
        StringBuilder strb = new StringBuilder();
        try {
            br = new BufferedReader(new FileReader(FilePath));
            while ((str = br.readLine()) != null) {
                strb.append(str).append("\n");
            }
        } catch (FileNotFoundException f){
            System.out.println(FilePath+" does not exist");
            return null;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strb.toString();
    }
}

Upvotes: 5

Views: 24898

Answers (3)

deHaar
deHaar

Reputation: 18568

You are not calling br.close() which means risking a resource leak. In order to reliably close the BufferedReader, you have two options:

using a finally block:

public String loader(String FilePath) {
    // initialize the reader with null
    BufferedReader br = null;
    String str = null;
    StringBuilder strb = new StringBuilder();

    try {
        // really initialize it inside the try block
        br = new BufferedReader(new FileReader(FilePath));
        while ((str = br.readLine()) != null) {
            strb.append(str).append("\n");
        }
    } catch (FileNotFoundException f) {
        System.out.println(FilePath + " does not exist");
        return null;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // this block will be executed in every case, success or caught exception
        if (br != null) {
            // again, a resource is involved, so try-catch another time
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return strb.toString();
}

using a try-with-resources statement:

public String loader(String FilePath) {
    String str = null;
    StringBuilder strb = new StringBuilder();

    // the following line means the try block takes care of closing the resource
    try (BufferedReader br = new BufferedReader(new FileReader(FilePath))) {
        while ((str = br.readLine()) != null) {
            strb.append(str).append("\n");
        }
    } catch (FileNotFoundException f) {
        System.out.println(FilePath + " does not exist");
        return null;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return strb.toString();
}

Upvotes: 19

TheWhiteRabbit
TheWhiteRabbit

Reputation: 1313

The code you wrote is indeed leaking resources as you're not closing your BufferedReader. The following snippet should do the trick:

public String loader(String filePath){
    String str = null;
    StringBuilder strb = new StringBuilder();
    // try-with-resources construct here which will automatically handle the close for you
    try (FileReader fileReader = new FileReader(filePath); 
         BufferedReader br = new BufferedReader(fileReader);){
        while ((str = br.readLine()) != null) {
            strb.append(str).append("\n");
        }
    }
    catch (FileNotFoundException f){
        System.out.println(filePath+" does not exist");
        return null;
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    return strb.toString();
}

If you're still having issues with this code, then yes, it's SonarQubes fault :-)

Upvotes: 3

Lino
Lino

Reputation: 19926

Seems like you just want to read all lines from a file. You could use this:

public String loader(String FilePath) {
    try(Scanner s = new Scanner(new File(FilePath).useDelimiter("\\A")) {
        return s.hasNext() ? s.next() : null;
    } catch(IOException e) {
        throw new UncheckedIOException(e);
    }
}

Upvotes: 2

Related Questions