30 November 2017


A known, and now kind of old feature of Java 7, but still isn't as popular as we wish.

Straightforward (and easy) implementation, where you need to declare resources (File, Stream) into parenthesis of "try" keyword:

private static void printFileJava7() throws IOException {

    try(FileInputStream input = new FileInputStream("hola.txt")) {

        int data = input.read();
        while(data != -1){
            System.out.print((char) data);
            data = input.read();

And JVM will have the responsibility to close those resources.

Beware: that the object in try() must implement AutoCloseable.

More info:




No comments :

My Blog List

Blog Archive


The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.