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;
} }
ObjectOutputStream 追加写入读取错误 - 自己的实现方案的更多相关文章
- ObjectOutputStream 追加写入读取错误
摘自http://blog.csdn.net/mitkey/article/details/50274543 问题描述: 用类ObjectOutputStream向文件写读对象时,碰到一个问题:新建一 ...
- Java读取txt文件和覆盖写入txt文件和追加写入txt
//创建文件 public static void createFile(File filename) { try { if(!filename.exists()) { filename.create ...
- python3读取、写入、追加写入excel文件
由于excel版本不同,python处理的时候选择的库页不同. 一.操作对应版本表格需要用到的库 1.操作xls格式的表格文件,需要用到的库如下: 读取:xlrd 写入:xlwt 修改(追加写入):x ...
- net快速写入/读取大量数据Postgresql
Postgresql快速写入/读取大量数据 http://www.cnblogs.com/podolski/p/7152144.html 环境及测试 使用.net驱动npgsql连接post数据库.配 ...
- 测开之路七十九:python 文件处理和对象的写入读取
"""处理文件:open(文件名, 模式,编码) 'r' 打开阅读(默认)'w' 打开写入,首先截断文件'x' 打开独占创建,如果文件已经存在则失败'a' 打开写入,追加 ...
- JS+Selenium+excel追加写入,使用python成功爬取京东任何商品~
之前一直是requests库做爬虫,这次尝试下使用selenium做爬虫,效率不高,但是却没有限制,文章是分别结合大牛的selenium爬虫以及excel追加写入操作而成,还有待优化,打算爬取更多信息 ...
- 安装vs2013以后,链接数据库总是报内存损坏,无法写入的错误
安装vs2013以后,链接数据库总是报内存损坏,无法写入的错误 这个错误几个月以前解决过一次,但是到又碰到的时候,竟然完全忘记当时怎么解决的了, 看来上了年纪记忆真是越来越不行了... 解决方案很简单 ...
- DataContext 数据在F5刷新频繁,会出现数据读取错误
DataContext 数据在F5刷新频繁,会出现数据读取错误 DataContext是 Linq to sql数据模型的底层数据库对象所有LInq数据表对象都是由它派生的, 只要建立一个数据库操作, ...
- Java基础知识强化之IO流笔记20:FileOutputStream写出数据实现换行和追加写入
1. 如何实现数据的换行? (1) package com.himi.fileoutputstream; import java.io.FileNotFoundException; import j ...
随机推荐
- 《Java程序员面试笔试宝典》之字符串创建与存储的机制是什么
在Java语言中,字符串起着非常重要的作用,字符串的声明与初始化主要有如下两种情况: (1) 对于String s1=new String("abc")语句与Strin ...
- 实现Android操作系统11种传感器介绍
在Android2.3 gingerbread系统中,google提供了11种传感器供应用层使用. #define SENSOR_TYPE_ACCELEROMETER 1 //加速度 #define ...
- BOOST::Signals2
/* Andy is going to hold a concert while the time is not decided. Eric is a fans of Andy who doesn't ...
- 关于mybatis插入数据库返回主键id
关于Sequence主键的数据库来说,如: <insert id="add" parameterType="vo.Category"> <se ...
- Git新建本地分支与远程分支关联问题:git branch --set-upstream
Git新建本地分支与远程分支关联问题:git branch --set-upstream git在本地新建分支, push到remote服务器上之后,再次pull下来的时候,如果不做处理会报以下提示: ...
- html进阶css(4)
盒子模型-边框 首先请看下图 <!doctype html> <html> <head> <meta charset="utf-8"> ...
- 整合spring2 + struts1.2 + hibernate3.2 .
1 可恶的myeclipse 为了开发方便,我选择了myeclipse,因为它集成了很多框架,而不致于自己去倒入很多lib.但就是因为这一点,成了我这次组合的致命伤.SSH因为其是开源框架,自 ...
- 如何只克隆git仓库中的一个分支?
git clone -b 例如: git clone -b 指定的分支名字
- label的for属性与inputde的id元素绑定
<form> <label for="male">Male</label> <input type="radio" n ...
- Python学习(三) 输出任意格式的字符串以及字符串的切片
在Python中想要输出一句话,如下 a='hello world' print a //打印出的是hello world print 'hello \n world' //打印出的是 //hello ...