Refactoring

Report a typo

Look at the following code:

BufferedWriter bufferedWriter = null;
try {
    bufferedWriter = new BufferedWriter(new FileWriter("veryImportantFile.txt"));
    bufferedWriter.write("It's okay");
} finally {
    if (bufferedWriter != null)
        bufferedWriter.close();
}

Imagine you want to replace this example with a similar one using try-with-resources. Select the letter that corresponds to the correct code snippet.

a)

try() {
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("veryImportantFile.txt"))
    bufferedWriter.write("It's okay");
}

b)

try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("veryImportantFile.txt"))) {
    bufferedWriter.write("It's okay");
}

c)

BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("veryImportantFile.txt"));
try (bufferedWriter.write("It's okay")) {
}
Select one option from the list
___

Create a free account to access the full topic