The try-with-resources statement automatically closes resources when the block finishes. You declare the resource inside parentheses after try:
try (FileReader reader = new FileReader("data.txt")) {
int ch = reader.read();
System.out.println((char) ch);
} catch (IOException e) {
System.out.println(e.getMessage());
}
Java calls reader.close() automatically when the try block ends, whether it completes normally or throws an exception. No finally block needed.
Compare this to the manual approach you saw earlier with the nested try-catch inside finally. Try-with-resources does the same thing in fewer lines and with less room for mistakes. You can declare multiple resources separated by semicolons, and Java closes them in reverse order.