It's not a secret anymore!
The moose likes Beginning Java and the fly likes What changes between writeObject and readObject? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What changes between writeObject and readObject?" Watch "What changes between writeObject and readObject?" New topic

What changes between writeObject and readObject?

Rebecca Witmer
Ranch Hand

Joined: Sep 10, 2004
Posts: 46
What the heck? I have tested this thoroughly and somewhere between serializing an object out to my file and reading it back in again my data is changing! I'll paste my code below.

What I've already done is the following:

I made a vector. Each time I loop through the code that puts stuff in the vector I clear out the vector. That seems to work. After the vector is made each time, I print it out to a file to see that each vector is different, as it should be. That worked. Then I tried printing out the vector on the line immediately before "writeObject". That worked. Then I tried printing out the vector on the line immediately after "writeObject". That worked too. Then I used the debugger to trace the contents of the vector on the line containing "writeObject". Beautiful, each time the vector is different. So I am thoroughly convinced that I ought to be getting different vectors serialized to my file.

Then when I read things back in from the file the vector is always the same. I can tell that from the debugger and printing the value to a file. I am pretty sure I am reading from the same file I am writing to because when I started having problems I added in a "writeInt" immediately before the "writeObject". This int is a counter. When I read back in with "readInt" the "readObject" it gets the correct number of integers, counting up to 5808, like it's supposed to. So I am convinced that I am progressing through the file holding the serialized data. But even though the int changes, the vector doesn't. I have asked some coworkers but no one knows the answer. DOH!

Any hint would be helpful.

void FindSDProducts(Vector sdMatrices, String wavFileName) throws IOException {
//Find the product of pair combinations of vectors of numbers which are the standard deviation from the mean energy for each segment in a section.

int counter = 0;
int dotPtr = wavFileName.indexOf(".");
wavFileName = wavFileName.substring(0, dotPtr);
wavFileName = wavFileName.concat(".SDProducts");

FileOutputStream fos = new FileOutputStream(wavFileName);
ObjectOutputStream outSDProducts = new ObjectOutputStream(fos);
Vector innerVector = new Vector();
for (int i = 0; i < sdMatrices.size(); i++) {
Matrix x = (Matrix) sdMatrices.elementAt(i);
for (int j = 0; j < sdMatrices.size(); j++) {
Matrix y = (Matrix) sdMatrices.elementAt(j);
Matrix xy = new Matrix(1, x.getColumnDimension());
for (int k = 0; k < x.getColumnDimension(); k++) {
double xElement = x.get(0, k);
double yElement = y.get(0, k);
double xyElement = xElement * yElement;
xy.set(0, k, xyElement);
innerVector.add(j, xy);
for (int m = 0; m < 4; m++) {
out.print("\n\t\tInner Vector just after it's been put in: " + m +
" \n\t\t");
Matrix q = (Matrix) innerVector.elementAt(m);
for (int n = 0; n < q.getColumnDimension(); n++) {
out.print(q.get(0, n) + " ");


boolean stuff = innerVector.isEmpty();
//System.err.print(counter + " ");

Vector thisVector = null;
int counter2 = 0;
boolean eof = false;
out.println("Here it is: ");
try {
System.err.println("reading from " + wavFileName);
FileInputStream fis = new FileInputStream(wavFileName);
ObjectInputStream in = new ObjectInputStream(fis);
while (!eof) {
counter2 = in.readInt();
thisVector = (Vector) in.readObject();
out.println("\n" + counter2);
//for (int m = 0; m < thisVector.size(); m++) {
for (int m = 0; m < 4; m++) {
out.print("\n\t\tSD Inner Vector matrix at: " + m + " \n\t\t");
Matrix x = (Matrix) thisVector.elementAt(m);
for (int n = 0; n < x.getColumnDimension(); n++) {
out.print(x.get(0, n) + " ");
x = null;
catch (EOFException ex) {
eof = true;
catch (IOException ex) {
out.println("Error reading pattern serialization file " + ex);
catch (ClassNotFoundException ex) {
out.println("Error reading pattern serialization file " + ex);


SCJP 1.4
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

# When a Serializable object is written with writeObject, then modified and written a second time, why is the modification missing when the stream is deserialized?

The ObjectOutputStream class keeps track of each object it serializes and sends only the handle if the object is written into the stream a subsequent time. This is the way it deals with graphs of objects. The corresponding ObjectInputStream keeps track of all of the objects it has created and their handles so when the handle is seen again it can return the same object. Both output and input streams keep this state until they are freed.

Alternatively, the ObjectOutputStream class implements a reset method that discards the memory of having sent an objecct, so sending an object again will make a copy.

Object Serialization FAQ

[How To Ask Questions On JavaRanch]
Rebecca Witmer
Ranch Hand

Joined: Sep 10, 2004
Posts: 46
The javadocs say:

Reset will disregard the state of any objects already written to the stream. The state is reset to be the same as a new ObjectOutputStream. The current point in the stream is marked as reset so the corresponding ObjectInputStream will be reset at the same point. Objects previously written to the stream will not be refered to as already being in the stream. They will be written to the stream again.

So does that mean that I have to reset the ObjectOutputStream each time I write the Vector? That seems to make strange things happen...
Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

well more than commenting on the process of serialization, I think having a look at the programming logic is a better Idea. well on the first looks there are two things that I discovered that might need your attention to review your code.

1. you are clearing all the elements in the Vector in the main For loop, which means by the time you are finished doing your loop, you inner vector will only have the last "matrix" Object.

2. you are writing to the Output Stream in the main for loop only, so that means you are essentially "over writing" the Object state over and over again, so the same effect as the 1 above.

these two could be the main reasons why you are having an upredictable beheaviour.

My suggestion is, first create the Vector that you want to serialize, once you are totally done, then open an Output Stream and simply write this Vector into it., which also optimizes your IO resources.
[ December 18, 2004: Message edited by: RajaniKanth Bhargava ]
I agree. Here's the link:
subject: What changes between writeObject and readObject?
jQuery in Action, 3rd edition