今日份代码,解决 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);
} }

最新文章

  1. ng-option指令使用记录,设置默认值需要注意
  2. 基本 sql语句
  3. 浅析-博客Ping服务
  4. linux下mongodb定时备份指定的集合
  5. 在ASP.NET MVC5 及 Visual Studio 2013 中为Identity账户系统配置数据库链接及Code-First数据库迁移
  6. 地址栏访问Action,后来方法执行两次
  7. php Mysql 和Mysqli数据库函数整合
  8. Python 学习笔记10
  9. oracle xe client 如何设置 tnsnames.ora(解决无法使用pl/sql developer的问题)
  10. 【原创】数据库基础之Mysql(1)常用命令
  11. Objective-C简介
  12. mysql GTID主从配置
  13. Android 一些关于 Activity 的技巧
  14. Java核心技术-映射
  15. HDOJ 1023 Train Problem II
  16. php缓存机制
  17. 《Effective Java》笔记-服务提供者框架
  18. 34-BigInteger详解
  19. bzoj 4961: 除除除
  20. jquery综合练习--模态对话框传值,删除,新增表格行

热门文章

  1. 最简单直接地理解Java软件设计原则之单一职责原则
  2. ShowDoc,APIDoc,可道云API,语雀-适合IT企业的文档工具
  3. linux下安装 zookeeper-3.4.9并搭建集群环境
  4. Dos命令思维导图
  5. 函数式编程(hashlib模块)
  6. js将金额转成大写金额
  7. TCP/IP__TCP协议
  8. shell脚本将字符串按指定分隔符切分成数组
  9. c++格式化输入输出以及操纵器的使用
  10. 2019牛客多校 Round4