java7增强的try语句关闭资源传统的关闭资源方式import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStrea
java7增强的try语句关闭资源
传统的关闭资源方式
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(); } } } }}
--结束END--
本文标题: 浅谈java7增强的try语句关闭资源
本文链接: https://lsjlt.com/news/225808.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0