gpt4 book ai didi

java - java中从二进制文件中读取对象

转载 作者:行者123 更新时间:2023-12-01 09:05:21 26 4
gpt4 key购买 nike

我有序列化对象并将其存储在二进制文件中的函数,还有另一个对象只是为了计算到目前为止我在文件中存储了多少个对象,所以我可以稍后使用计数器来浏览文件并将其反序列化并读取它。这是我的写入功能:

Class2 dad = new Class2(DadName.getText(), DadLastName.getText());

Class1 son = new Class1(FirstName.getText(), dad, "Session");

// Write object to file
FileOutputStream outStream = new FileOutputStream(new File("MainStore.dat"), true);
ObjectOutputStream objectOutputFile = new ObjectOutputStream(outStream);
objectOutputFile.writeObject(son);
objectOutputFile.close();


// Update the objectcounter file by
// Maintenance is a class I use just to save object contain information about number of files,increase them, decrease them.

Maintenance check = new Maintenance();
FileInputStream inStream = new FileInputStream("Counter.dat");
ObjectInputStream objectInput = new ObjectInputStream(inStream);
check = (Maintenance) objectInput.readObject();
inStream.close();

check.increaseObject();

FileOutputStream outStream1 = new FileOutputStream("Counter.dat");
ObjectOutputStream objectOutputFile1 = new ObjectOutputStream(outStream1);

objectOutputFile1.writeObject(check);
objectOutputFile1.close();

前面的函数工作得很好,下一步是从文件中读取对象的函数,我使用 Counter.dat 来查看存储在 MainStore.dat 上的对象数量,它给了我正确的数量。这是读取函数:

// Read Counter file to know how many stored objects to go through them
Maintenance check = new Maintenance();
FileInputStream inStream = new FileInputStream("Counter.dat");
ObjectInputStream objectInput = new ObjectInputStream(inStream);
check = (Maintenance) objectInput.readObject();
int Counter1 = check.getObjectsNumber(); // function I created in the Maintenance class to return number of object stored
inStream.close();


// Read the stored objects
//here is my problem begin

FileInputStream inStream2 = new FileInputStream("MainStore.dat");
ObjectInputStream objectInputFile = new ObjectInputStream(inStream2);

// Create array of objects
Class1[] arrayOfObjects = new Class1[Counter1];

// Read the serialized objects from the file.
for (int i = 0; i < Counter1; i++) {
arrayOfObjects[i] = (Class1) objectInputFile.readObject(); // here is the error pointed by compiler
}
objectInputFile.close();

for (int i = 0; i < 2; i++) {
// here I should have array of all objects ready to read details

}

除了最后一点“//从中读取序列化对象”之外,一切看起来都很好。它给了我这个错误

Caused by: java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at registerController.CheckPlaceAvailabilityAction(registerController.java:120)
... 58 more

当我只读取一个对象进行测试时,它工作得很好并为我返回第一个存储的对象,当我尝试读取所有存储的对象时发生错误。

最佳答案

显然,您无法像您一样透明地连接/附加ObjectOutputStreams

参见the docs :

[The] constructor writes the serialization stream header to the underlying stream

因此,一个新的流将首先写入一些 header ,这将需要一个新的输入流来读取。否则,输入流将尝试将 header 作为序列化对象读取,但会失败,因为这些 header 不是有效的序列化对象数据。

关于java - java中从二进制文件中读取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41324062/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com