Javatry-with-resources
Java Close Resources
When working with files, streams, or other resources, it is important toclose 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 aboutfiles andstreams in detail. These topics will come in the next chapters. For now, just focus on howtry-with-resources works.
In older code, you had to close "resources" manually by calling theirclose() 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."); } }}Run Example »Java try-with-resources
Since Java 7, you can usetry-with-resources. It is a special form oftry that works with resources (likefiles andstreams). The resource is declared inside parenthesestry(...), 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.
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 tohandle files in Java.

