Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ObjectInputStream覆盖所有数据_Java_Objectinputstream_Objectoutputstream - Fatal编程技术网

Java ObjectInputStream覆盖所有数据

Java ObjectInputStream覆盖所有数据,java,objectinputstream,objectoutputstream,Java,Objectinputstream,Objectoutputstream,看起来objectoutputstreams可能充满了陷阱。目前,我相信outputstream工作正常,但当我尝试从文件中读入时,我正在使用的hashmap的所有数据都被覆盖。 大多数时候,hashmap值的int不是0,但是当我从文件中检索对象时,hashmap的每个键值都是0 下面是我的问题的伪/实代码 写作: HashMap<State, int> temp= new HashMap <state, int>(); try{ FileOutput

看起来objectoutputstreams可能充满了陷阱。目前,我相信outputstream工作正常,但当我尝试从文件中读入时,我正在使用的hashmap的所有数据都被覆盖。 大多数时候,hashmap值的int不是0,但是当我从文件中检索对象时,hashmap的每个键值都是0

下面是我的问题的伪/实代码

写作:

HashMap<State, int> temp= new HashMap <state, int>();

    try{
    FileOutputStream fileOut = new   FileOutputStream("/Users/_____/Documents/hash.ser");
             ObjectOutputStream out = new ObjectOutputStream(fileOut);
             out.reset(); //added before and after for testing.

             //iterating through hash to see if everything is correct before outputting. (and it is)
             Set set = hash.entrySet();
             // Get an iterator
             Iterator i = set.iterator();
             // Display elements
             while(i.hasNext()) {
                Map.Entry me = (Map.Entry)i.next();
                temp.put((state)me.getKey(), (int)me.getValue());

                //output is as expected
                System.out.println("Serializing "+me.getValue() + "\n\n\n");

           }


             //using flush and reset hoping for effect.
             //tried using write(temp)
             out.writeUnshared(temp);
             out.flush();
             out.reset();
             out.close();

             fileOut.close();

          }catch(IOException i){
              i.printStackTrace();
          }
输入经过大量调试后,错误似乎出现在这里

 try
          {
             FileInputStream fileIn = new FileInputStream("/Users/_____/Documents/hash.ser");
             ObjectInputStream in = new ObjectInputStream(fileIn);


         HashMap<state, int> temp= new HashMap <state, int>();

         //hash = (HashMap) in.readObject();

         temp=(HashMap) in.readObject();
         Set set = temp.entrySet();
         // Get an iterator
         Iterator i = set.iterator();
         // Display elements
         while(i.hasNext()) {
            Map.Entry me = (Map.Entry)i.next();
            hash.put((state)me.getKey(), (int)me.getValue());

            //Always is 0.
            System.out.println("Getting IN "+me.getValue() + "\n\n\n");

       }



         in.close();
         fileIn.close();
      }catch(IOException i)
      {
         i.printStackTrace();
         return;
      }catch(ClassNotFoundException c)
      {
         System.out.println("class not found");
         c.printStackTrace();
         return;
      }

所以你真正的问题是什么?如果您提供了一个我们可以自己运行的MVCE,这会有所帮助。关于为什么InputStream或OutputStream正在更改所有键值,我是否有任何直接的遗漏?我不能说。如果我/我们可以自己运行代码,我们可能会更好地处理症状是什么,从这项工作回到问题的原因是什么。现在,最合理的解释是,这些条目一开始并不存在。另一种可能是您已经将State类的字段声明为transient。。。或者错误地实现了自定义序列化方法。如何确保它们不是瞬态的?我从来没用过那个关键词。我对我的.equals/hashCode非常有信心,但如果这就是我所能做到的,我将继续研究它。