本篇博客灵感来自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;
} }

ObjectOutputStream 追加写入读取错误 - 自己的实现方案的更多相关文章

  1. ObjectOutputStream 追加写入读取错误

    摘自http://blog.csdn.net/mitkey/article/details/50274543 问题描述: 用类ObjectOutputStream向文件写读对象时,碰到一个问题:新建一 ...

  2. Java读取txt文件和覆盖写入txt文件和追加写入txt

    //创建文件 public static void createFile(File filename) { try { if(!filename.exists()) { filename.create ...

  3. python3读取、写入、追加写入excel文件

    由于excel版本不同,python处理的时候选择的库页不同. 一.操作对应版本表格需要用到的库 1.操作xls格式的表格文件,需要用到的库如下: 读取:xlrd 写入:xlwt 修改(追加写入):x ...

  4. net快速写入/读取大量数据Postgresql

    Postgresql快速写入/读取大量数据 http://www.cnblogs.com/podolski/p/7152144.html 环境及测试 使用.net驱动npgsql连接post数据库.配 ...

  5. 测开之路七十九:python 文件处理和对象的写入读取

    """处理文件:open(文件名, 模式,编码) 'r' 打开阅读(默认)'w' 打开写入,首先截断文件'x' 打开独占创建,如果文件已经存在则失败'a' 打开写入,追加 ...

  6. JS+Selenium+excel追加写入,使用python成功爬取京东任何商品~

    之前一直是requests库做爬虫,这次尝试下使用selenium做爬虫,效率不高,但是却没有限制,文章是分别结合大牛的selenium爬虫以及excel追加写入操作而成,还有待优化,打算爬取更多信息 ...

  7. 安装vs2013以后,链接数据库总是报内存损坏,无法写入的错误

    安装vs2013以后,链接数据库总是报内存损坏,无法写入的错误 这个错误几个月以前解决过一次,但是到又碰到的时候,竟然完全忘记当时怎么解决的了, 看来上了年纪记忆真是越来越不行了... 解决方案很简单 ...

  8. DataContext 数据在F5刷新频繁,会出现数据读取错误

    DataContext 数据在F5刷新频繁,会出现数据读取错误 DataContext是 Linq to sql数据模型的底层数据库对象所有LInq数据表对象都是由它派生的, 只要建立一个数据库操作, ...

  9. Java基础知识强化之IO流笔记20:FileOutputStream写出数据实现换行和追加写入

    1.  如何实现数据的换行? (1) package com.himi.fileoutputstream; import java.io.FileNotFoundException; import j ...

随机推荐

  1. TypeScript 素描 - 模块解析、声明合并

    模块解析 模块解析有两种方式 相对方式  也就是以/或 ./或-/开头的,比如import jq  from "/jq" 非相对方式  比如 import model  from ...

  2. js调用swift相册DEMO(网易新闻)

    关键代码 window.location.href = 'tg:///openCamera' css body{ } img{ width:100%; } #mainTitle{ text-align ...

  3. 【DataStructure】Description and usage of queue

    [Description] A queue is a collection that implements the first-in-first-out protocal. This means th ...

  4. 改进的简单Tooltips显示

    使用js简单改进了Tooltips的显示效果,可进一步使用CSS对改进的Tooltips进行美化. 前台布局代码: <asp:Panel ID="Panel1" runat= ...

  5. MongoDB学习笔记06

    在shell中删除一个集合,执行db.test.drop()或者db.runCommand({"drop":"test"}),在MongoDB中命令其实是作为一 ...

  6. 几句话弄清楚Java参数传值还是传引用

    最近刷题做了一些算法题,对于在递归函数调用的时候什么时候传入值,什么时候传入引用有疑问,在网上搜索了一下,得出了一下三条总结: 1.对象就是传引用 2.原始类型就是传值 3.String,Intege ...

  7. 数据库对于null值的处理

    对于null值的处理,不同的数据库的处理函数是不同的,这里列举了部分数据库对于null的处理函数以及使用: Oracle:是用函数nvl(), ----nvl(chinese,0);如果语文成绩为nu ...

  8. Cocoapods依赖管理

    对于iOS App的开发,几乎都采用了Cocoapods来管理第三方库,那么对于开发人员来说,这是必备技能,必须要掌握如何使用.这篇文章就是介绍如何安装和使用CocoaPods的. 简单来说,就是专门 ...

  9. UIView的一些常用属性和方法

    UIView的一些常用属性和方法 1. UIView的属性 UIView继承自UIResponder,拥有touches方法. - (instancetype)initWithFrame:(CGRec ...

  10. XML 解析中,如何排除控制字符

    XML 解析中,如何排除控制字符 今天在解析一个中文的 XML时,始终报错 PCDATA invalid Char value 21 in Entity ,查询了一下这个 21 的ascii 值,发现 ...