Are you still writing try-catch-finally? To use the try-with-resources gracefully shut down

Are you still writing try-catch-finally? To use the try-with-resources gracefully shut down the stream, right
foreword
development, we often need to close some of the resources in the final. Read and write files such as streaming and other common, we will close the resource in the final finally in. But such an approach is very simple. In fact, early in JDK1.7 it has introduced a try-with-resources to close the way of resources, we have to look at the experience today simplicity of the try-with-resources are.

Some examples of closed legacy resources
in older versions of the wording in (in fact, now there are a lot of programmers is so written), resources are placed in the finally block to shut down, as follows:

@Test
public void test4() {
    InputStream inputStream = null;
    try {
        inputStream = new FileInputStream("D:\\head.jpg");
        // do something
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

The trouble is that such an approach, we need to close the resource in the finally block, so inputStream can only be defined outside of the try block. Until then, we need to do step sentenced empty, avoiding empty because inputStream caused by a null pointer exception. The wording is very tedious.

try-with-resources

The same function, if the try-with-resources, it will make the code very simple:

@Test
public void test5() {
    try (InputStream inputStream = new FileInputStream("D:\\head.jpg")) {
        byte[] bytes = inputStream.readAllBytes();
        // do something
    } catch (IOException e) {
        e.printStackTrace();
    }
}

try-with-resources usage was in the back with a try keyword brackets, the need to close the resource definition in parentheses. After executing the try block will automatically freed resources.

What resources can be try-with-resources automatically closes
Not all resources can be try-with-resources automatically shut down, and only realized the class java.lang.AutoCloseable interface can be automatically shut down. If you do not realize java.lang.AutoCloseable class definition in parentheses try, and it will be an error in the compiler.

For example, the MyResource a custom class, an error is reported in parentheses defined: class java.lang.AutoCloseable prompted.
Are you still writing try-catch-finally?  To use the try-with-resources gracefully shut down

Custom can be automatically shut down classes
we can also write your own class can be automatically shut down the try-with-resources class, we need only to realize java.lang.AutoCloseable interface can be.

class MyResource implements java.lang.AutoCloseable {

    @Override
    public void close() {
        System.out.println("调用了close方法");
    }
}
@Test
public void test5() {
    try (InputStream inputStream = new FileInputStream("D:\\head.jpg");
         MyResource myResource = new MyResource()) {
        byte[] bytes = inputStream.readAllBytes();
        // do something
    } catch (IOException e) {
        e.printStackTrace();
    }
}

After execution, the output will "call the close method"
Are you still writing try-catch-finally?  To use the try-with-resources gracefully shut down

Summarizes the
try-with-resources can make the code more concise and less error-prone. Compared to the traditional wording try-catch-finally, apparently try-with-resources advantages more, at least not a problem because no judge finally close the resource space caused by a null pointer.

Guess you like

Origin blog.51cto.com/14207399/2415077