java7增強(qiáng)的try語句關(guān)閉資源
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名注冊、網(wǎng)頁空間、營銷軟件、網(wǎng)站建設(shè)、肅州網(wǎng)站維護(hù)、網(wǎng)站推廣。傳統(tǒng)的關(guān)閉資源方式
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 { //創(chuàng)建對象輸出流 oos = new ObjectOutputStream(new FileOutputStream("b.bin")); //創(chuàng)建對象輸入流 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(); } } } } }