Try-with-Resources Statement in Java


Use try-with-resources for automatic resource management, ensuring resources are closed after execution.

Source Code

try (Scanner scanner = new Scanner(new File("test.txt"))) {
    while (scanner.hasNext()) {
        System.out.println(scanner.nextLine());
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments