Zest.

Java: try-with-resources

A short, tested Java example for try-with-resources. Copy it and adjust the names and paths for your project.

Java
try (var reader = Files.newBufferedReader(Path.of("data.txt"))) {
    String line;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
} catch (IOException e) {
    e.printStackTrace();
}

Find it in Zest

Search for “java try-with-resources” in Zest and this example appears above the results, with other matches to choose from. Searches stay private.

More Java examples

All Java examples