0
public StringBuffer readFile(final File inputFile) {
    String tempLine; // variable declaration
    Logger log = Logger.getLogger("Error Message");
    try {
        final FileReader fileReader = new FileReader(inputFile);
        final BufferedReader bufferedReader = new BufferedReader(fileReader);
        final StringBuffer content = new StringBuffer();
        while((tempLine=bufferedReader.readLine())!=null) {
            content.append(tempLine);
            content.append(System.getProperty("line.separator"));
        }
    }
    catch(FileNotFoundException e) {
        log.log(Level.WARNING, "File not found", e);
    }
    catch (IOException e) {
        log.log(Level.WARNING, "Couldn't Read file", e);
    }
    finally {
        bufferedReader.close();
        fileReader.close();
    }
    return content;
}

The variable fileReader and bufferedReader declared as final in try block cannot be used in finally block. I can't declare them outside try block, because they might throw exception. I want the variables to be final too.

Ivar
  • 6,138
  • 12
  • 49
  • 61
Rohan RM
  • 157
  • 1
  • 2
  • 10
  • 4
    Maybe you could explain why you think you need any variable in your entire code snippet to be `final`. – Tim Biegeleisen Jun 18 '18 at 12:04
  • 7
    Since all you really do in the finally block is closing the resources this is probably a typical use case for [The try-with-resources Statement](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) – OH GOD SPIDERS Jun 18 '18 at 12:06
  • @Marit This will not compile since you cannot be sure that the assingment (which is in the `try` block) is executed. The compiler will give you an error stating that the variables `fileReader`, `bufferedReader` and `content` may not have been initialized. – Turing85 Jun 18 '18 at 12:10
  • 1
    @TimBiegeleisen I'm guessing it's code style, according to this: https://stackoverflow.com/questions/137868/using-the-final-modifier-whenever-applicable-in-java – rechandler Jun 18 '18 at 12:27

2 Answers2

1

If you use at least Java 7 you can use try-with-resources Statement to close your resources. Here's the code:

public StringBuffer readFile(final File inputFile) {
    String tempLine; // variable declaration
    Logger log = Logger.getLogger("Error Message");
    try (final FileReader fileReader = new FileReader(inputFile)) {
        try (final BufferedReader bufferedReader = new BufferedReader(fileReader)) {
            final StringBuffer content = new StringBuffer();
            while((tempLine=bufferedReader.readLine())!=null) {
                content.append(tempLine);
                content.append(System.getProperty("line.separator"));
            }

            return content;
        }
    } catch (FileNotFoundException e) {
        log.log(Level.WARNING, "File not found", e);
    } catch (IOException e) {
        log.log(Level.WARNING, "Couldn't Read file", e);
    }
    // return null or throw exception;
}
rechandler
  • 756
  • 8
  • 22
0

Since Java 7, you can write things like that:

public StringBuffer readFile(final File inputFile) {
    String tempLine; // variable declaration
    Logger log = Logger.getLogger("Error Message");
    final StringBuffer content = new StringBuffer();
    try (final FileReader fileReader = new FileReader(inputFile);
            final BufferedReader bufferedReader = new BufferedReader(fileReader)){        
        while((tempLine=bufferedReader.readLine())!=null) {
            content.append(tempLine);
            content.append(System.getProperty("line.separator"));
        }
    }
    catch(FileNotFoundException e) {
        log.log(Level.WARNING, "File not found", e);
    }
    catch (IOException e) {
        log.log(Level.WARNING, "Couldn't Read file", e);
    }
    return content;
}

Here fileReader and bufferedReader are implicitly closed.

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24