Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>stepancheg is right, are you sure you're not rereading the first serialized object?</p> <p>The following sample works. If you can create something similar that doesn't, please post it here. import java.io.*;</p> <pre><code>public class SerialDeserial { public static void main(String[] args) { try { ChangingObject obj = new ChangingObject(); obj.foo=1; // Write it ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("test.foo")); os.writeObject(obj); os.flush();os.close(); // Read the object ObjectInputStream is = new ObjectInputStream(new FileInputStream("test.foo")); obj = (ChangingObject)is.readObject(); System.out.println("obj.foo is "+obj.foo); // Change it obj.foo=2; // Write it os = new ObjectOutputStream(new FileOutputStream("test.foo")); os.writeObject(obj); os.flush();os.close(); // Read the object is = new ObjectInputStream(new FileInputStream("test.foo")); obj = (ChangingObject)is.readObject(); System.out.println("obj.foo is "+obj.foo); } catch (Exception e) { e.printStackTrace(); } } } class ChangingObject implements Serializable { public int foo; } </code></pre> <p><strong>Edit:</strong> If I take your altered (well, not really) example I still get a correct output (with 2 instead of as the second output). You suggestion to do reset should make no difference as you are reopening the file and starting from the beginning both when reading and writing. I think your problems lies elsewhere (what OS are you using?, which file system? etc.) It should just work, sorry.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload