对象串行化

当调用对象的程序执行结束以后,对象就随之消亡。但是,有时希望将对象的状态记录下来,以便于恢复,这个过程,就叫做对象串行化。

要理解对象串行化,就需要了解两个关键字Serializable和transient。一个对象,要能够串行化,就必须实现Serializable接口。而transient则表明,某个属性不属于串行化的一部分。

如下代码,是一个串行化的示例。将Test中的username串行化到硬盘,而password则否。然后,再用ObjectInputStream将对象从硬盘中读取并恢复,并读取其中的username属性。

对象的串行化并不难,主要是实现Serializable接口,并且配合ObjectOutputStream和ObjectInputStream使用。

 1 import java.io.*;
 2 /*
 3  * author:Tammy Pi
 4  * function:对象的串行化
 5  */
 6 public class SerializableTest implements Serializable{
 7 
 8     class Test implements Serializable{
 9         
10         private String username;
11         //表明password不是对象串行化的一部分
12         private transient String password;
13         
14         public Test(String username,String password){
15             
16             this.username = username;
17             this.password = password;
18         }
19         
20         public String toString() {
21             
22             return username+","+password;
23         }
24     }
25     
26     public void test(){
27         
28         Test t = new Test("tammypi","1988");
29         System.out.println(t.toString());
30         
31         //将t持久化到硬盘
32         ObjectOutputStream os = null;
33         ObjectInputStream oi = null;
34         try {
35             os = new ObjectOutputStream(new FileOutputStream("c:\\Test.out"));
36             os.writeObject(t);
37             os.flush();
38             os.close();
39             os = null;
40             
41             oi = new ObjectInputStream(new FileInputStream("c:\\Test.out"));
42             try {
43                 Test t1 = (Test) oi.readObject();
44                 System.out.println(t1.toString());
45             } catch (ClassNotFoundException e) {
46                 // TODO Auto-generated catch block
47                 e.printStackTrace();
48             }
49             
50         } catch (FileNotFoundException e) {
51             // TODO Auto-generated catch block
52             e.printStackTrace();
53         } catch (IOException e) {
54             // TODO Auto-generated catch block
55             e.printStackTrace();
56         } finally{
57             
58             if(os!=null){
59                 
60                 try {
61                     os.close();
62                 } catch (IOException e) {
63                     // TODO Auto-generated catch block
64                     e.printStackTrace();
65                 }
66             }
67             if(oi!=null){
68                 
69                 try {
70                     oi.close();
71                 } catch (IOException e) {
72                     // TODO Auto-generated catch block
73                     e.printStackTrace();
74                 }
75             }
76         }
77     }
78     
79     public static void main(String[] args){
80         
81         SerializableTest s = new SerializableTest();
82         s.test();
83     }
84 }

 

转:http://blog.csdn.net/rongyongfeikai2/article/details/7945149

 

posted @ 2016-01-18 11:11  tvxqpurpleline  阅读(202)  评论(0编辑  收藏  举报