English 中文(简体)
禁用序列化缓存
原标题:
  • 时间:2009-03-23 11:49:39
  •  标签:

有没有一种方法可以在Java中禁用序列化对象的缓存?

我有这个情境:

  1. I have an object which is Serializable, I am serializing it, deserializing it, values are OK.
  2. On the same object, I am changing some value, I am serializing it, deserializing it, values are NOT OK, values are same as the first initially loaded.

好像序列化器在缓存值,是吗?

谢谢 (xiè xie)

抄了这个例子从“弗雷德里克”,并根据我的情况做了调整:

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"));
            ChangingObject objDummy = (ChangingObject)is.readObject();
            System.out.println("objDummy.foo is "+objDummy.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"));
            objDummy = (ChangingObject)is.readObject();
            System.out.println("objDummy.foo is "+objDummy.foo); // this returns "1" insted of "2"


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class ChangingObject implements Serializable {
    public int foo;
}  
最佳回答

ObjectOutputStream.reset 的中文翻译为“重置”。

你还可以使用writeUnshared方法写入对象,但是这种方法是浅层的,因此引用的对象仍然会被共享。

当然,不变的对象通常是一种胜利。

问题回答

Stepancheg 是对的,你确定你没有重新阅读第一个序列化的对象吗?

The following sample works. If you can create something similar that doesn t, please post it here. import java.io.*;

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;
}

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

序列化器没有缓存。

你应该展示一个最小的例子,以便重现你的情况。





相关问题
热门标签