java 附加到 ObjectOutputStream(在不关闭流的情况下写入多个对象)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6578973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:27:42  来源:igfitidea点击:

Appending to ObjectOutputStream (writing multiple objects w/o closing stream)

javaserializationobjectoutputstream

提问by MoveFast

Desclaimer My question is different from two following links

免责声明我的问题与以下两个链接不同

Question 1

问题 1

Question 2

问题2

    public class AppendableObjectOutputStream extends ObjectOutputStream {
      public AppendableObjectOutputStream(OutputStream out) throws IOException {
        super(out);
      }

      @Override
      protected void writeStreamHeader() throws IOException {}
}
  • The problem with above solutions is that they do not support writing multiple objects to appendable stream w/o closing the stream.
  • If I open appendable stream, write multiple objects - then at time of reading I can read only first object properly and on trying to read second object, I get EOF exception.
  • If I proceed the way like write on object to appendable stream, close stream. Then again open stream, write another object close and so on. This way I am able to read multiple objects properly.

        fileOutputStream = new FileOutputStream("abc.dat",true);
         outputBuffer = new BufferedOutputStream(fileOutputStream);
         objectStream = new AppendableObjectOutputStream(outputBuffer);
         BucketUpdate b1 = new BucketUpdate("getAllProducts1",null,"1",null);
         BucketUpdate b2 = new BucketUpdate("getAllProducts2",null,"2",null);
         BucketUpdate b3 = new BucketUpdate("getAllProducts3",null,"3",null);
         objectStream.writeObject(b1);
         objectStream.writeObject(b2);
         objectStream.writeObject(b3);
         objectStream.close();
    
  • 上述解决方案的问题在于,它们不支持在不关闭流的情况下将多个对象写入可附加流。
  • 如果我打开可追加流,写入多个对象 - 然后在读取时我只能正确读取第一个对象,而在尝试读取第二个对象时,我得到 EOF 异常。
  • 如果我像在对象上写入可附加流那样继续,请关闭流。然后再次打开流,关闭另一个对象,依此类推。这样我就可以正确读取多个对象。

        fileOutputStream = new FileOutputStream("abc.dat",true);
         outputBuffer = new BufferedOutputStream(fileOutputStream);
         objectStream = new AppendableObjectOutputStream(outputBuffer);
         BucketUpdate b1 = new BucketUpdate("getAllProducts1",null,"1",null);
         BucketUpdate b2 = new BucketUpdate("getAllProducts2",null,"2",null);
         BucketUpdate b3 = new BucketUpdate("getAllProducts3",null,"3",null);
         objectStream.writeObject(b1);
         objectStream.writeObject(b2);
         objectStream.writeObject(b3);
         objectStream.close();
    

回答by Suraj Chandran

Calling ObjectOutputStream.reset() after writing each object will fix this.

在写入每个对象后调用 ObjectOutputStream.reset() 将解决此问题。

回答by Sergey Aslanov

If you check question you mentioned, you will see that you have to use AppendableObjectOutputStreamonly to appendobjects to file, if file already contains some objects. For empty file you have to use ordinary ObjectOutputStreambecause the header should be written to the beginning in this case.

如果您检查您提到的问题,您将看到您必须AppendableObjectOutputStream仅使用对象附加到文件,如果文件已经包含一些对象。对于空文件,您必须使用普通文件,ObjectOutputStream因为在这种情况下应将标头写入开头。