【问题标题】:Read objects from binary files in java从java中的二进制文件中读取对象
【发布时间】:2016-12-25 19:50:31
【问题描述】:

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

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

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

【问题讨论】:

  • 如果可以的话,您可能想发布一个 MVCE(stackoverflow.com/help/mcve)。
  • 为什么要持久化计数器?您确定您的文件在测试期间没有在某处损坏吗?我建议删除它并重新开始一个新文件。

标签: java file oop serialization javafx


【解决方案1】:

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

the docs:

[The] 构造函数将序列化流标头写入 底层流

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 2014-03-26
    • 2015-04-03
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多