0

I've been looking into Serializing Objects so that I can send Objects through networking easily and quickly. There's one big Issue in this though. After I initialize a value in my array, the serialized Object for that array will always stay as the initialized number. I'm not even sure if this is the case as I'm pretty sure ints have a default value of 0. So wouldn't it stay as 0 instead of what I change it to?

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
    FileOutputStream ostr = new FileOutputStream(new File("ObjectFile.yeh"));
    FileInputStream istr = new FileInputStream(new File("ObjectFile.yeh"));
    ObjectOutputStream out= new ObjectOutputStream(ostr);
    int[] hi = new int[5];
    hi[0]=3;
    out.writeUTF("This is an Array");
    out.writeObject(hi);
    hi[0]=5;
    out.writeObject(hi);
    out.writeUTF("Array Over");
    out.close();
    ObjectInputStream in = new ObjectInputStream(istr);
    System.out.println(in.readUTF());
    int[] h = (int[]) in.readObject();
    System.out.println(h[0]);
    int[] j = (int[]) in.readObject();
    System.out.println(j[0]);
    System.out.println(in.readUTF());
    in.close();
}

When I run the following code it prints

This is an Array
3
3
Array Over

How should I solve this problem?

Update:

After further experimentation I found that after an Object is Serialized once, it's serial doesn't seem to change. As shown here I edit the String array and it's contents don't change after being sent through the Object Stream

    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
    FileOutputStream ostr = new FileOutputStream(new File("ObjectFile.yeh"));
    FileInputStream istr = new FileInputStream(new File("ObjectFile.yeh"));
    ObjectOutputStream out= new ObjectOutputStream(ostr);
    String[] hi = new String[5];
    hi[0]="Hi";
    out.writeUTF("This is an Array");
    out.writeObject(hi);
    hi[0]="Hello";
    hi[1]="Hoopla";
    out.writeObject(hi);
    out.writeUTF("Array Over");
    out.close();
    ObjectInputStream in = new ObjectInputStream(istr);
    System.out.println(in.readUTF());
    String[] h = (String[]) in.readObject();
    System.out.println(h[0]+" "+h[1]);
    String[] j = (String[]) in.readObject();
    System.out.println(j[0]+" "+j[1]);
    System.out.println(in.readUTF());
    in.close();
}

And this prints

This is an Array
Hi null
Hi null
Array Over

1 Answers1

2

Reset your output stream before changing it.

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
    FileOutputStream ostr = new FileOutputStream(new File("ObjectFile.yeh"));
    FileInputStream istr = new FileInputStream(new File("ObjectFile.yeh"));
    ObjectOutputStream out= new ObjectOutputStream(ostr);
    int[] hi = new int[5];
    hi[0]=3;
    out.writeUTF("This is an Array");
    out.writeObject(hi);
    out.reset(); // Add this line
    hi[0]=5;
    out.writeObject(hi);
    out.writeUTF("Array Over");
    out.close();
    ObjectInputStream in = new ObjectInputStream(istr);
    System.out.println(in.readUTF());
    int[] h = (int[]) in.readObject();
    System.out.println(h[0]);
    int[] j = (int[]) in.readObject();
    System.out.println(j[0]);
    System.out.println(in.readUTF());
    in.close();
}
ayip
  • 2,473
  • 1
  • 19
  • 30