public class TestObject {
 public static void main(String[] args) {
		// TODO Auto-generated method stub
		//对象的序列化--将对象以二进制的形式输出(没有确定输出到哪儿去)
//		ArrayList<Student> al = new ArrayList<Student>();
//		Student stu0 = new Student("HJ", 25,"四川","成都","九眼桥第三桥洞");
//		Student stu1 = new Student("YP", 27,"四川","成都","九眼桥第2桥洞");
//		al.add(stu0);
//		al.add(stu1);
//		
//		ObjectOutputStream oos = null;
//		
//		try {
//			oos = new ObjectOutputStream(new FileOutputStream("student.data"));
//			oos.writeObject(al);
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}finally{
//			if(oos != null){
//				try {
//					oos.close();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//		}
		
		
		//反序列化--将输入的二进制流转换为对象(同样不确定二进制流的数据源)
		ArrayList<Student> al = null;
		ObjectInputStream ois = null;
		
		try {
			//使用了装饰器模式
			ois = new ObjectInputStream(new FileInputStream("student.data"));		
			al = (ArrayList<Student>)ois.readObject();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if(ois != null){
				try {
					ois.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		System.out.println(al.get(0));
		System.out.println(al.get(1));
	}
}
感觉好绕 头痛
原文:http://www.cnblogs.com/whj1986556646/p/5598089.html