今日份代碼,解決 ObjectOutputStream 屢次寫入發生重複寫入相同數據的問題java
核心區別以下:app
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); } }