java7增強try語句關閉資源

來源:互聯網
上載者:User

標籤:null   and   輸入資料流   href   tin   sea   序列   塊代碼   jdbc編程   

java7增強try語句關閉資源

傳統的關閉資源方式

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream; class Student implements Serializable {  private String name;   public Student(String name) {    this.name = name;  }} public class test2 {  public static void main(String[] args) throws Exception {    Student s = new Student("WJY");    Student s2 = null;    ObjectOutputStream oos = null;    ObjectInputStream ois = null;    try {      //建立對象輸出資料流      oos = new ObjectOutputStream(new FileOutputStream("b.bin"));      //建立對象輸入資料流      ois = new ObjectInputStream(new FileInputStream("b.bin"));      //序列化java對象      oos.writeObject(s);      oos.flush();      //還原序列化java對象      s2 = (Student) ois.readObject();    } finally { //使用finally塊回收資源      if (oos != null) {        try {          oos.close();        } catch (Exception ex) {          ex.printStackTrace();        }      }      if (ois != null) {        try {          ois.close();        } catch (Exception ex) {          ex.printStackTrace();        }      }    }  }}

使用finally塊來關閉實體資源,保證關閉操作總是會被執行。

關閉每個資源之前首先保證引用該資源的引用變數不為null。

為每一個實體資源使用單獨的try...catch塊來關閉資源,保證關閉資源時引發的異常不會影響其他資源的關閉。

以上方式導致finally塊代碼十分臃腫,程式的可讀性降低。

java7增強try語句關閉資源

為瞭解決以上傳統方式的問題, Java7新增了自動關閉資源的try語句。它允許在try關鍵字後緊跟一對圓括弧,裡面可以聲明、初始化一個或多個資源,此處的資源指的是那些必須在程式結束時顯示關閉的資源(資料庫連接、網路連接等),try語句會在該語句結束時自動關閉這些資源。

?
12345678910111213141516171819 public class test2 {  public static void main(String[] args) throws Exception {    Student s = new Student("WJY");    Student s2 = null;    try (//建立對象輸出資料流        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("b.bin"));        //建立對象輸入資料流        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("b.bin"));    )  {      //序列化java對象      oos.writeObject(s);      oos.flush();      //還原序列化java對象      s2 = (Student) ois.readObject();    }   }}

自動關閉資源的try語句相當於包含了隱式的finally塊(用於關閉資源),因此這個try語句可以既沒有catch塊,也沒有finally塊。

注意:

被自動關閉的資源必須實現Closeable或AutoCloseable介面。(Closeable是AutoCloseable的子介面,Closeeable介面裡的close()方法聲明拋出了IOException,;AutoCloseable介面裡的close()方法聲明拋出了Exception)

被關閉的資源必須放在try語句後的圓括弧中聲明、初始化。如果程式有需要自動關閉資源的try語句後可以帶多個catch塊和一個finally塊。

Java7幾乎把所有的“資源類”(包括檔案IO的各種類,JDBC編程的Connection、Statement等介面……)進行了改寫,改寫後的資源類都實現了AutoCloseable或Closeable介面

java7增強try語句關閉資源

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.