Learn Java
import com.learnjava.filehandling.TryWithResourcesIo;

try-with-resources for File I/O in Java

@Concept

File-handling classes like FileReader, FileWriter, BufferedReader, and FileInputStream all implement AutoCloseable, which makes them a natural fit for try-with-resources — declaring one in a try's parentheses guarantees it gets closed once the block ends, whether that block finished normally or an exception interrupted it partway through. This matters specifically for files because an unclosed file handle can leak an operating-system resource, and on some systems can even leave a file locked or its written content not fully flushed to disk if the program doesn't close it properly before exiting.

Multiple resources can be declared in the same try's parentheses, separated by semicolons, and Java closes them automatically in the reverse of their declaration order. The last resource opened is the first one closed, which matters when resources depend on each other, like a BufferedReader that wraps a FileReader.

@HowItWorks
1

File classes implement AutoCloseable

FileReader, FileWriter, BufferedReader, and similar all support try-with-resources directly.

2

Guaranteed closing

A resource declared in try's parentheses is closed once the block ends, exception or not.

3

Why it matters for files specifically

An unclosed file can leak an OS-level resource or leave written content not fully flushed to disk.

4

Multiple resources, one try

Separated by semicolons inside the same parentheses, all managed automatically together.

5

Reverse-order closing

The most recently opened resource closes first, correctly unwinding dependencies like a reader wrapped around another reader.

@Example
import java.io.FileWriter;
import java.io.BufferedWriter;
 
public class TryWithResourcesFileDemo {
public static void main(String[] args) throws Exception {
try (FileWriter fw = new FileWriter("log.txt");
BufferedWriter bw = new BufferedWriter(fw)) {
bw.write("Log entry recorded");
}
System.out.println("Both resources closed automatically");
}
}
Output
Both resources closed automatically
1Two resources are declared in the same try's parentheses, separated by a semicolon: fw first, then bw wrapping it.
2bw.write(...) writes through the buffered wrapper, which internally uses fw underneath.
3Once the block finishes, bw is closed first, then fw — the reverse of the order they were opened in.
4The final println confirms execution continues normally, having relied on both being closed automatically with no explicit finally block.
@UseCases
Guaranteeing a file writer is properly closed and flushed, even if an exception interrupts writing
Managing a wrapped pair of resources, like a BufferedWriter around a FileWriter, correctly and automatically
Avoiding a file-handle leak in a program that opens many files over its lifetime
Replacing a manual try/finally block that closed file resources by hand with cleaner, automatic resource management

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.