如何将两个 Java 序列化对象重新链接在一起? [英] How can I link two Java serialised objects back together?

查看:33
本文介绍了如何将两个 Java 序列化对象重新链接在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时(实际上很多)我们会在 Java 中遇到两个对象指向同一事物的情况.现在,如果我们分别序列化它们,序列化形式具有对象的单独副本是非常合适的,因为应该可以打开一个而没有另一个.但是,如果我们现在反序列化它们,我们会发现它们仍然是分开的.有什么办法可以将它们重新连接在一起吗?

Sometimes (quite a lot, actually) we get a situation in Java where two objects are pointing to the same thing. Now if we serialise these separately it is quite appropriate that the serialised forms have separate copies of the object as it should be possible to open one without the other. However if we now deserialise them both, we find that they are still separated. Is there any way to link them back together?

示例如下.

public class Example {

 private static class ContainerClass implements java.io.Serializable {
  private ReferencedClass obj;
  public ReferencedClass get() {
   return obj;
  }
  public void set(ReferencedClass obj) {
   this.obj = obj;
  }
 }

 private static class ReferencedClass implements java.io.Serializable {
  private int i = 0;
  public int get() {
   return i;
  }
  public void set(int i) {
   this.i = i;
  }
 }

 public static void main(String[] args) throws Exception {
  //Initialise the classes
  ContainerClass test1 = new ContainerClass();
  ContainerClass test2 = new ContainerClass();
  ReferencedClass ref = new ReferencedClass();

  //Make both container class point to the same reference
  test1.set(ref);
  test2.set(ref);

  //This does what we expect: setting the integer in one (way of accessing the) referenced class sets it in the other one
  test1.get().set(1234);
  System.out.println(Integer.toString(test2.get().get()));

  //Now serialise the container classes
  java.io.ObjectOutputStream os = new java.io.ObjectOutputStream(new java.io.FileOutputStream("C:\\Users\\Public\\test1.ser"));
  os.writeObject(test1);
  os.close();
  os = new java.io.ObjectOutputStream(new java.io.FileOutputStream("C:\\Users\\Public\\test2.ser"));
  os.writeObject(test2);
  os.close();

  //And deserialise them
  java.io.ObjectInputStream is = new java.io.ObjectInputStream(new java.io.FileInputStream("C:\\Users\\Public\\test1.ser"));
  ContainerClass test3 = (ContainerClass)is.readObject();
  is.close();
  is = new java.io.ObjectInputStream(new java.io.FileInputStream("C:\\Users\\Public\\test2.ser"));
  ContainerClass test4 = (ContainerClass)is.readObject();
  is.close();

  //We expect the same thing as before, and would expect a result of 4321, but this doesn't happen as the referenced objects are now separate instances
  test3.get().set(4321);
  System.out.println(Integer.toString(test4.get().get()));
 }

}

推荐答案

readResolve() 方法 允许这样做(当然,首先你必须定义你将如何决定哪些对象是相同的").但是将两个对象序列化到同一个文件中会容易得多——ObjectOut/InputStream 会记录它已序列化/反序列化的所有对象,并且只会存储和返回对它已经看到的对象的引用.

The readResolve() method allows this (of course, first you have to define how you're going decide which objects are meant to be "the same"). But much easier would be serializing both objects into the same file - the ObjectOut/InputStream keeps a record of all objects it has serialized/deserialized and will only store and return references to objects it has already seen.

这篇关于如何将两个 Java 序列化对象重新链接在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆