ObjectOutputStream 追加写入读取错误 - 自己的实现方案

 

 本篇博客灵感来自http://blog.csdn.net/chenssy/article/details/13170015

 

 问题描述、问题出现的原因、尝试解决办法,请参见鄙人上一编博客。

上一编文章解决ObjectOutputStream 追加写入读取错误问题的方法是自定义了一个ObjectOutputStream子类,我觉得不如用匿名内部类实现方便,于是自我研究写出以下两种方案。个人更推崇方案二

 

方案一:

 1 package packa;
 2 
 3 import java.io.*;
 4 
 5 class ObjectInputOutputStream2
 6 {
 7     public static void main(String[] args)throws Exception
 8     {
 9         writeObj();
10         readObj();
11     }
12 
13     private static void writeObj()throws Exception
14     {
15         final File f = new File("person.object");
16         ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.object", true))
17         {
18             protected  void writeStreamHeader()throws IOException
19             {
20                 if (!f.exists() || (f.exists() && 0 == f.length()))
21                 {
22                     super.writeStreamHeader();
23                 }
24             }
25         };
26 
27         oos.writeObject(new Person("liu", 30, "kr"));
28         oos.writeObject(new Person2("liu", "kr", "kr2"));
29         oos.writeObject(new Person3(10, 30, 50));
30 
31         oos.close();
32     }
33 
34     private static void readObj()throws Exception
35     {
36         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.object"));
37 
38         try
39         {
40             while(true)
41             {
42                 System.out.println(ois.readObject());    
43             }
44         }
45         catch (Exception e)
46         {
47             System.out.println(e.toString());
48         }
49 
50         ois.close();
51     }
52 }

 

方案二:

 1 package packa;
 2 
 3 import java.io.*;
 4 
 5 class ObjectInputOutputStream
 6 {
 7     public static void main(String[] args)throws Exception
 8     {
 9         writeObj();
10         readObj();
11     }
12 
13     static ObjectOutputStream  getObjectOutputStreamInstance(final OutputStream os, final File f) throws IOException
14     {
15         return  new ObjectOutputStream(os)
16         {
17             protected  void writeStreamHeader()throws IOException
18             {
19                 if (!f.exists() || (f.exists() && 0 == f.length()))
20                 {
21                     super.writeStreamHeader();
22                 }
23             }
24         };
25     }
26 
27     private static void writeObj()throws Exception
28     {
29         ObjectOutputStream oos = getObjectOutputStreamInstance(new FileOutputStream("person.object", true), new File("person.object"));
30         oos.writeObject(new Person("liu", 30, "kr"));
31         oos.writeObject(new Person2("liu", "kr", "kr2"));
32         oos.writeObject(new Person3(10, 30, 50));
33 
34         oos.close();
35     }
36 
37     private static void readObj()throws Exception
38     {
39         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.object"));
40 
41         try
42         {
43             while(true)
44             {
45                 System.out.println(ois.readObject());    
46             }
47         }
48         catch (Exception e)
49         {
50             System.out.println(e.toString());
51         }
52 
53         ois.close();
54     }
55 }

 

 1 package packa;
 2 
 3 import java.io.*;
 4 
 5 class Person implements Serializable
 6 {
 7     public static final long serialVersionUID = 44L;
 8 
 9     String name;
10     transient int age;
11     static String country = "cn";
12 
13     Person(String name, int age, String country)
14     {
15         this.name = name;
16         this.age = age;
17         this.country = country;
18     }
19 
20     public String toString()
21     {
22         return name + " " + age + " " + country;
23     }
24 
25 }
26 class Person2 implements Serializable
27 {
28     public static final long serialVersionUID = 45L;
29 
30     String name;
31     String country;
32     String country2;
33 
34     Person2(String name, String country, String country2)
35     {
36         this.name = name;
37         this.country = country;
38         this.country2 = country2;
39     }
40 
41     public String toString()
42     {
43         return name + " " + country + " " + country2;
44     }
45 
46 }
47 class Person3 implements Serializable
48 {
49     public static final long serialVersionUID = 46L;
50 
51     int age;
52     int age2;
53     int age3;
54 
55     Person3(int age, int age2, int age3)
56     {
57         this.age = age;
58         this.age2 = age2;
59         this.age3 = age3;
60     }
61 
62     public String toString()
63     {
64         return age + " " + age2 + " " + age3;
65     }
66 
67 }

 

posted @ 2016-11-29 23:48  LiuYanYGZ  阅读(422)  评论(0编辑  收藏  举报