代碼必定得寫的優雅一點 html
JDK7以前資源的關閉姿式:java
/**
* jdk7之前關閉流的方式
*
* @author hetiantian
* */
public class CloseResourceBefore7 {
private static final String FileName = "file.txt";
public static void main(String[] args) throws IOException {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(FileName);
char c1 = (char) inputStream.read();
System.out.println("c1=" + c1);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}
複製代碼
JDK7及之後關閉資源的正確姿式:try-with-resource Resource的定義:全部實現了 java.lang.AutoCloseable 接口(其中,它包括實現了 java.io.Closeable 的全部對象),能夠使用做爲資源。 簡單Demo進行證明: 實現java.lang.AutoCloseable接口的Resource類:api
/**
* 資源類
*
* @author hetiantian
* */
public class Resource implements AutoCloseable {
public void sayHello() {
System.out.println("hello");
}
@Override
public void close() throws Exception {
System.out.println("Resource is closed");
}
}
複製代碼
測試類CloseResourceIn7.javabash
/**
* jdk7及之後關閉流的方式
*
* @author hetiantian
* */
public class CloseResourceIn7 {
public static void main(String[] args) {
try(Resource resource = new Resource()) {
resource.sayHello();
} catch (Exception e) {
e.printStackTrace();
}
}
}
複製代碼
打印結果:oracle
hello
Resource is closed
複製代碼
當存在多個打開資源的時候: 資源二Resource2.javaide
/**
* 資源2
*
* @author hetiantian
* */
public class Resource2 implements AutoCloseable {
public void sayhello() {
System.out.println("Resource say hello");
}
@Override
public void close() throws Exception {
System.out.println("Resource2 is closed");
}
}
複製代碼
測試類CloseResourceIn7.java測試
/**
* jdk7及之後關閉流的方式
*
* @author hetiantian
* */
public class CloseResourceIn7 {
public static void main(String[] args) {
try(Resource resource = new Resource(); Resource2 resource2 = new Resource2()) {
resource.sayHello();
resource2.sayhello();
} catch (Exception e) {
e.printStackTrace();
}
}
}
複製代碼
打印結果:spa
hello
Resource say hello
Resource2 is closed
Resource is closed
複製代碼
即便資源不少,代碼也能夠寫的很簡潔,若是用jdk7以前的方式去關閉資源,那麼資源越多,用fianl關閉資源時嵌套也就越多code
那麼它的底層原理又是怎樣的呢,由皮皮甜獨家揭祕優雅關閉資源背後的密碼 查看編譯的class文件CloseResourceIn7.class:cdn
public class CloseResourceIn7 {
public CloseResourceIn7() {
}
public static void main(String[] args) {
try {
Resource resource = new Resource();
Throwable var2 = null;
try {
resource.sayHello();
} catch (Throwable var12) {
var2 = var12;
throw var12;
} finally {
if (resource != null) {
if (var2 != null) {
try {
resource.close();
} catch (Throwable var11) {
var2.addSuppressed(var11);
}
} else {
resource.close();
}
}
}
} catch (Exception var14) {
var14.printStackTrace();
}
}
}
複製代碼
能夠發現編譯之後生成了try-catch-finally語句塊 finally中的var2.addSuppressed(var11);是否是有疑問🤔️ 其實這麼作是爲了處理異常屏蔽的 咱們將代碼修改一下 資源Resource.java
/**
* 資源類
*
* @author hetiantian
* */
public class Resource implements AutoCloseable {
public void sayHello() throws Exception {
throw new Exception("Resource throw Exception");
}
@Override
public void close() throws Exception {
throw new Exception("Close method throw Exception");
}
}
複製代碼
兩個方法裏面都拋出異常
測試類CloseResourceIn7.java
/**
* jdk7及之後關閉流的方式
*
* @author hetiantian
* */
public class CloseResourceIn7 {
public static void main(String[] args) {
try {
errorTest();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void errorTest() throws Exception {
Resource resource = null;
try {
resource = new Resource();
resource.sayHello();
}
finally {
if (resource != null) {
resource.close();
}
}
}
}
複製代碼
打印結果:
java.lang.Exception: Close method throw Exception
at com.shuwen.Resource.close(Resource.java:15)
at com.shuwen.CloseResourceIn7.errorTest(CloseResourceIn7.java:27)
at com.shuwen.CloseResourceIn7.main(CloseResourceIn7.java:12)
複製代碼
只打印了最後出現的異常【異常屏蔽】這樣會給開發人員排查錯誤帶來必定的困難 咱們換成try-with-resource方法實現CloseResourceIn7.java
/**
* jdk7及之後關閉流的方式
*
* @author hetiantian
* */
public class CloseResourceIn7 {
public static void main(String[] args) {
try {
errorTest();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void errorTest() throws Exception {
try(Resource resource = new Resource()) {
resource.sayHello();
}
}
}
複製代碼
打印信息:
java.lang.Exception: Resource throw Exception
at com.shuwen.Resource.sayHello(Resource.java:10)
at com.shuwen.CloseResourceIn7.errorTest(CloseResourceIn7.java:20)
at com.shuwen.CloseResourceIn7.main(CloseResourceIn7.java:12)
Suppressed: java.lang.Exception: Close method throw Exception
at com.shuwen.Resource.close(Resource.java:15)
at com.shuwen.CloseResourceIn7.errorTest(CloseResourceIn7.java:21)
... 1 more
複製代碼
能夠發現,異常信息中多了一個Suppressed的提示,告訴咱們這個異常其實由兩個異常組成,Close method throw Exception這個異常是被Suppressed【屏蔽】的異常
總結 怎麼樣,是否是很簡單呢,若是學會了話