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.
Original Statement
This article first appeared in the headline number [Happyjava]. Happy Nuggets address: juejin.im/user/5cc289... , Happy personal blog :( blog.happyjava.cn ) [ blog.happyjava.cn ]. Welcome to reprint, but declared by this section shall be retained.
Some examples of closed legacy resources
In the old 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 shut down
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.
Custom classes can be automatically turned off
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"
to sum up
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.