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 ...
随机推荐
- js网页返回页面顶部的小方法
咳咳,在网页出现滚动条的时候,许多网站会在右下角出现一个图标,点击可以回到页面顶部 本文就记录下js实现代码: 1.在html页面body添加dom元素 <img src="toTop ...
- java多态的理解----部分非原创
所谓多态,其实就是对于同一件事情,不同的对象要采取不同的行为,或者同一个对象在不同的情况下需要采取不同的行为方式. 不同的对象要采取不同的行为: 这有两种实现方式:接口实现和子类重新父类方法.这两种实 ...
- [Cycle.js] Hyperscript as our alternative to template languages
Usually we use template languages like Handlebars, JSX, and Jade to create. One simple way we can cr ...
- NET基础课--Linq第二讲
这一讲,来说说集合.因为linq主要用于对数据源进行查询,集合是最常见的数据源. 集合 形式: 数组,列表List<T> Arraylist等. 特点: 可通过索引或键访问.可进行fore ...
- 一些基础的.net用法
一.using 用法 using 别名设置 using 别名 = System.web 当两个不同的namespace里有同名的class时.可以用 using aclass = namespace1 ...
- .Net Memory -- Windbg基本命令
命令 解释 .cls 清空命令窗口屏幕 .load dllfullpath 加载debugger扩展dll如SOS sosex psscor. .loadby dll moduleName 加载deb ...
- 一、Autofac入门
想要将autofac集成到你的应用程序中需要经过如下步骤: 1.使用控制翻转(IoC)的思想架构你的应用程序: 2.添加autofac引用: 3.在应用程序入口...(At application s ...
- C#代码启用事务锁Transaction进行一系列提交回滚操作
一.前言 因为很多人一般进行一系列相关数据库操作都是在存储过程里面,而且在存储过程用锁的写法也是很简单的,在这篇文章主要介绍一下C#后台代码用锁进行一系列事务操作,我建立一个简单的winform程序, ...
- Python学习笔记2(控制语句)
1.if条件语句 if(表达式): 语句1 else: 语句2 2.if...elif...else判断语句 if(表达式1):语句1 elif(表达式2):语句2 ... elif(表达式n):语句 ...
- Reading source code
software is a system built up of many parts rebuild that decomposition see the patterns in codes is ...