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

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

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

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

方案一:

 package packa;

 import java.io.*;

 class ObjectInputOutputStream2
{
public static void main(String[] args)throws Exception
{
writeObj();
readObj();
} private static void writeObj()throws Exception
{
final File f = new File("person.object");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.object", true))
{
protected void writeStreamHeader()throws IOException
{
if (!f.exists() || (f.exists() && 0 == f.length()))
{
super.writeStreamHeader();
}
}
}; oos.writeObject(new Person("liu", 30, "kr"));
oos.writeObject(new Person2("liu", "kr", "kr2"));
oos.writeObject(new Person3(10, 30, 50)); oos.close();
} private static void readObj()throws Exception
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.object")); try
{
while(true)
{
System.out.println(ois.readObject());
}
}
catch (Exception e)
{
System.out.println(e.toString());
} ois.close();
}
}

方案二:

 package packa;

 import java.io.*;

 class ObjectInputOutputStream
{
public static void main(String[] args)throws Exception
{
writeObj();
readObj();
} static ObjectOutputStream getObjectOutputStreamInstance(final OutputStream os, final File f) throws IOException
{
return new ObjectOutputStream(os)
{
protected void writeStreamHeader()throws IOException
{
if (!f.exists() || (f.exists() && 0 == f.length()))
{
super.writeStreamHeader();
}
}
};
} private static void writeObj()throws Exception
{
ObjectOutputStream oos = getObjectOutputStreamInstance(new FileOutputStream("person.object", true), new File("person.object"));
oos.writeObject(new Person("liu", 30, "kr"));
oos.writeObject(new Person2("liu", "kr", "kr2"));
oos.writeObject(new Person3(10, 30, 50)); oos.close();
} private static void readObj()throws Exception
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.object")); try
{
while(true)
{
System.out.println(ois.readObject());
}
}
catch (Exception e)
{
System.out.println(e.toString());
} ois.close();
}
}
 package packa;

 import java.io.*;

 class Person implements Serializable
{
public static final long serialVersionUID = 44L; String name;
transient int age;
static String country = "cn"; Person(String name, int age, String country)
{
this.name = name;
this.age = age;
this.country = country;
} public String toString()
{
return name + " " + age + " " + country;
} }
class Person2 implements Serializable
{
public static final long serialVersionUID = 45L; String name;
String country;
String country2; Person2(String name, String country, String country2)
{
this.name = name;
this.country = country;
this.country2 = country2;
} public String toString()
{
return name + " " + country + " " + country2;
} }
class Person3 implements Serializable
{
public static final long serialVersionUID = 46L; int age;
int age2;
int age3; Person3(int age, int age2, int age3)
{
this.age = age;
this.age2 = age2;
this.age3 = age3;
} public String toString()
{
return age + " " + age2 + " " + age3;
} }
上一篇:go gmp --- goroutine创建源码分析


下一篇:vs2010 调试 调用堆栈 窗口