简体   繁体   English

从Java中的二进制文件读取对象

[英]Read objects from binary files in java

I have function to serialize object and store it in a binary file, and also another object just to count how many objects I have stored on the file so far, so I can use the counter later to go through the file and deserialize it and read it. 我具有序列化对象并将其存储在二进制文件中的功能,还有另一个对象只是为了计算到目前为止文件中存储了多少个对象,因此以后可以使用计数器对文件进行反序列化并读取它。 Here is my write function: 这是我的写功能:

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();

The previous function works just fine, the next step is function to read read the objects back from file, I use Counter.dat to see the number of Objects stored on MainStore.dat and it gives me the right number. 上一个功能运行良好,下一步是从文件读取对象的功能,我使用Counter.dat查看MainStore.dat上存储的对象数,它给出了正确的数字。 Here is the read function: 这是读取功能:

// 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

}

Everything looks fine except the last bit "// Read the serialized objects from". 除了最后一位“ //从中读取序列化的对象”之外,其他所有内容看起来都不错。 it gives me this error 它给了我这个错误

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

and When I tested it with only reading one object, it worked perfect and return the first stored object for me, the error happened when I try to read all stored objects. 当我仅读取一个对象进行测试时,它可以完美工作并为我返回第一个存储的对象,当我尝试读取所有存储的对象时发生了错误。

Apparently, you cannot transparently concatenate/append ObjectOutputStreams like you do. 显然,您不能像您那样透明地串联/附加ObjectOutputStreams

See the docs : 文档

[The] constructor writes the serialization stream header to the underlying stream [The]构造函数将序列化流标头写入基础流

So a new stream will write some headers first, which would require a new input stream to read. 因此,新的流将首先写入一些标头,这将需要新的输入流来读取。 Otherwise, the input stream will try to read the headers as a serialized object and will fail because those headers are not valid serialized object data. 否则,输入流将尝试将标头读取为序列化对象,并且将失败,因为这些标头不是有效的序列化对象数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM