【java】ObjectOutputStream & ObjectInputStream 多次写入发生重复写入相同数据的问题
2024-09-01 00:47:50
今日份代码,解决 ObjectOutputStream 多次写入发生重复写入相同数据的问题
核心区别如下:
package com.sxd.swapping.objoutputstream; import org.junit.Test; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List; /**
* ObjectOutputStream 写对象到序列化文件中
* ObjectInputStream 读对象到反序列化文件中
*/
public class SerializeTest { /**
* 写生成序列化文件
*
* 对同一个 共享对象 做重复性的多次写入 建议使用writeObject + reset方法 组合使用 做重置动作 (即深克隆效果)下面这个 方法写入的 是 test1 test2 test3 三条。
* 对于 非共享对象, 做多次重复性的写入 可以使用 writeUnshared 方法(即浅克隆效果)
*
*
*
* 而 单独使用 writeObject方法,会导致 第二次写入的对象 依旧是 第一次写对的对象。即 流不对被重置,导致下面这个 方法写入的 是错误的 test1 test2 test1 test2 四条。
*
* @throws Exception
*/
@Test
public void test3() throws Exception {
List<Student> students = new ArrayList<>();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("C:/Users/ouiyuio/Desktop/students.ser")));
for (int i = 1; i <= 3; i++) {
Student student = new Student(i, "test" + i);
students.add(student); if (students.size() >= 2) {
// objectOutputStream.writeUnshared(students);
objectOutputStream.writeObject(students);
objectOutputStream.reset(); //使用 reset保证写入重复对象时进行了重置操作
students.clear();
}
}
if (students.size() > 0) {
objectOutputStream.writeObject(students);
objectOutputStream.flush();
}
objectOutputStream.writeObject(null);
objectOutputStream.close();
} /**
* 反序列化读取文件内容
* @throws Exception
*/
@Test
public void test4() throws Exception {
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("C:/Users/ouiyuio/Desktop/students.ser")));
List<Student> students = (List<Student>) objectInputStream.readObject();
int count = 0;
while (students != null) {
count += students.size();
for (Student student : students) {
System.out.println(student);
}
students = (List<Student>) objectInputStream.readObject();
}
objectInputStream.close();
System.out.println(count);
} @Test
public void test5() throws Exception {
int count = 0;
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("C:/Users/ouiyuio/Desktop/students.ser")));
List<Student> students1 = (List<Student>) objectInputStream.readObject();
count += students1.size();
for (Student student : students1) {
System.out.println(student);
}
List<Student> students2 = (List<Student>) objectInputStream.readObject();
count += students2.size();
for (Student student : students2) {
System.out.println(student);
}
objectInputStream.close();
System.out.println(count);
} }
最新文章
- ng-option指令使用记录,设置默认值需要注意
- 基本 sql语句
- 浅析-博客Ping服务
- linux下mongodb定时备份指定的集合
- 在ASP.NET MVC5 及 Visual Studio 2013 中为Identity账户系统配置数据库链接及Code-First数据库迁移
- 地址栏访问Action,后来方法执行两次
- php Mysql 和Mysqli数据库函数整合
- Python 学习笔记10
- oracle xe client 如何设置 tnsnames.ora(解决无法使用pl/sql developer的问题)
- 【原创】数据库基础之Mysql(1)常用命令
- Objective-C简介
- mysql GTID主从配置
- Android 一些关于 Activity 的技巧
- Java核心技术-映射
- HDOJ 1023 Train Problem II
- php缓存机制
- 《Effective Java》笔记-服务提供者框架
- 34-BigInteger详解
- bzoj 4961: 除除除
- jquery综合练习--模态对话框传值,删除,新增表格行