try-with-resources
使用以下:/** * 將二進制文件讀取出來 * * @param filePath 文本絕對路徑 * @return * @throws FileNotFoundException * @throws IOException */ public static byte[] getReaderToBinary(String filePath) throws FileNotFoundException, IOException { int bufferSize = 4096; // 設置緩衝區大小 byte buffer[] = new byte[bufferSize]; // 緩衝區字節數組 File sourceFile = new File(filePath); byte[] b = null; try(InputStream fis = new FileInputStream(sourceFile); BufferedInputStream bis = new BufferedInputStream(fis, bufferSize); ) { int len = bis.available(); b = new byte[len]; bis.read(b, 0, len); } return b; }
上述例子參考地址:html
* 做者:這我的太懶了 * 來源:CSDN * 原文:https://blog.csdn.net/qq_35546153/article/details/83421506 * 版權聲明:本文爲博主原創文章,轉載請附上博文連接!
以前,採用try-catch-finally
,則較爲繁瑣不直觀,甚至偶爾會忘記將其關閉,以下:java
InputStream fis = null; BufferedInputStream bis = null; try{ fis = new FileInputStream(sourceFile); bis = new BufferedInputStream(fis, bufferSize); int len = bis.available(); b = new byte[len]; bis.read(b, 0, len); }finally { if(fis != null){ fis.close(); } if(bis != null){ bis.close(); } }
try-with-resources的用法就是,在try關鍵字的後面跟一個括號,把須要關閉的資源定義在括號內。在try塊執行完以後會自動的釋放掉資源。數據庫
可是必須注意,並非全部的指望關閉的代碼均可以放進其中,只有實現了java.lang.AutoCloseable接口的類,才能夠被自動關閉。數組
eg:上述代碼示例中的 InputStream
,其定義以下:網絡
public abstract class InputStream implements Closeable
而 Closeable
,其定義以下(from jdk1.5版本):ide
/** * A {@code Closeable} is a source or destination of data that can be closed. * The close method is invoked to release resources that the object is * holding (such as open files). * * @since 1.5 */ public interface Closeable extends AutoCloseable
說明:測試
try-with-resource
語法是自 JDK7 新增的。try-catch-finally
寫法場景以 打開了外部資源 居多:this
如有自定義的需求:.net
public class CustomResource implements java.lang.AutoCloseable { @Override public void close() { System.out.printf("調用了[%s]的close方法\n", this.getClass().getName()); } public static void main(String[] args) { try (CustomResource customResource = new CustomResource();){ // do something System.out.println("do something"); throw new RuntimeException("測試拋出未知異常"); } } }
控制檯輸出以下:code
do something Exception in thread "main" java.lang.RuntimeException: 測試拋出未知異常 at com.xxx.main(CustomResource.java:22) 調用了[com.xxx.CustomResource]的close方法
說明:即便try
塊中拋出異常,也不影響resource
的關閉
注意:
public class CustomResource implements java.lang.AutoCloseable { public CustomResource(){ throw new RuntimeException("構造器異常:"+ this.getClass().getName()); } @Override public void close() { System.out.printf("調用了[%s]的close方法\n", this.getClass().getName()); throw new RuntimeException("close方法異常:"+ this.getClass().getName()); } public static void main(String[] args) { try (CustomResource customResource = new CustomResource();){ // do something System.out.println("do something"); } catch (Exception e){ System.out.println("do catch"); e.printStackTrace(); } } }
控制檯輸出以下:
do catch java.lang.RuntimeException: 構造器異常:com.xxx.CustomResource at com.xxx.CustomResource.<init>(CustomResource.java:13) at com.xxx.CustomResource.main(CustomResource.java:24)
解釋說明(參考地址:http://www.javashuo.com/article/p-gxawejwn-hv.html):
try-with-resource時,若是對外部資源的處理和對外部資源的關閉均遭遇了異常,「關閉異常」將被抑制,「處理異常」將被拋出,但「關閉異常」並無丟失,而是存放在「處理異常」的被抑制的異常列表中。