7ktsyq
Last Updated: November 21, 2017
·
6.57K
· markm

IDisposable in Java

Java 7 introduces an equivalent pattern to C#'s IDisposable pattern, AutoClosable, or try-with-resources. For example:

try (BufferedReader br = new 
        BufferedReader(new FileReader(path))) {
    return br.readLine();
}

More info on the interface here: http://docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html

More info on usage here: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

Prior to Java 7 you are unfortunately stuck with using standard try/finally and manually releasing resources.