将一个静态对象分别写入两个不同的流中,然后分别又读出来,但是却得到的是不同的对象呢,对象写入到流中的到底是什么呢?

解决方案 »

  1.   

    反序列化创建的对象当然会不同,就算value一样,hashcode也不同的。
      

  2.   

    class TO implements Serializable{
    static TO tt= new TO();
    }public class ObjectStream {
    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
    ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("object.txt"));
    ObjectOutputStream os2 = new ObjectOutputStream(new FileOutputStream("object2.txt"));
    ObjectInputStream is=new ObjectInputStream(new FileInputStream("object.txt"));
    ObjectInputStream is2=new ObjectInputStream(new FileInputStream("object2.txt"));
    os.writeObject(TO.tt);
    os2.writeObject(TO.tt);
    TO bb=(TO)is.readObject();
    TO cc=(TO)is2.readObject();
    System.out.println(bb==cc);
    is.close();
    os.close();
    is2.close();
    os2.close(); }}