🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Java Notes
Topic #87

Java try-with-resources


Java Close Resources

When working with files, streams, or other resources, it is important to close them after use. If you forget to close a resource, it may keep using memory or even prevent you from opening the file again until the program ends.

Note: You have not yet learned about files and streams in detail. These topics will come in the next chapters. For now, just focus on how try-with-resources works.

In older code, you had to close "resources" manually by calling their close() method:

Example

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
  public static void main(String[] args) {
    try {
      FileOutputStream output = new FileOutputStream("filename.txt");
      output.write("Hello".getBytes());
      output.close();  // must close manually
      System.out.println("Successfully wrote to the file.");
    } catch (IOException e) {
      System.out.println("Error writing file.");
    }
  }
}

Java try-with-resources

Since Java 7, you can use try-with-resources. It is a special form of try that works with resources (like files and streams). The resource is declared inside parentheses try(...), and Java will close it automatically when the block finishes - even if an error occurs.

Example (try-with-resources)

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
  public static void main(String[] args) {
    // resource is opened inside try()
    try (FileOutputStream output = new FileOutputStream("filename.txt")) {
      output.write("Hello".getBytes());
      // no need to call close() here
      System.out.println("Successfully wrote to the file.");
    } catch (IOException e) {
      System.out.println("Error writing file.");
    }
  }
}

Why use try-with-resources?

  • Safer - resources are always closed, even if an exception occurs.
  • Cleaner - no need to write close() calls.
  • Shorter code - less boilerplate, easier to read.

Note: Rule of thumb: Whenever you work with files, streams, or database connections, use try-with-resources to make sure they are closed properly.

Next: Learn about how to handle files in Java.

Want to go beyond the notes?

Join CodingNow 2.0's Java course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Java try-with-resources – FAQs

Quick answers about learning Java try-with-resources in Java.

This free note from CodingNow 2.0 explains Java try-with-resources in Java — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Java topic on CodingNow 2.0, including Java try-with-resources, is 100% free with no signup required.
With focused practice, most students grasp Java try-with-resources in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now