0

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

2
  • You might want to post an MVCE(stackoverflow.com/help/mcve) if you can. Commented Dec 25, 2016 at 20:14
  • Why do you want to persist the counter? Are you sure your file did not get corrupted somewhere during testing? I suggest deleting it and starting over with a new file. Commented Dec 25, 2016 at 20:59

1 Answer 1

1

Apparently, you cannot transparently concatenate/append ObjectOutputStreams like you do.

See the docs:

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

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.