Write and read multiple objects to file

后端 未结 3 936
后悔当初
后悔当初 2020-12-03 16:24

I am designing an handwriting application for android.

I would like to write information (class LogInfo) into a log file, every time the user presses th

相关标签:
3条回答
  • 2020-12-03 16:43

    there is a way to save multiple objects in one file: you should first make an object array (Object [] objects) and then put your objects one-by-one as an Object in this array and then write this array using writeObject(objects) method. good luck.

    0 讨论(0)
  • 2020-12-03 16:49

    @EJP is right.it cannot append to an existing file created with an ObjectOutputStream. You can do the following steps to fix it: 1. keep ObjectOutputStream object reference 2. after writeObject() having been called, do not call close() 3. provide a method to close ObjectOutputStream.

    0 讨论(0)
  • 2020-12-03 16:50
    1. You can't append to an existing file created with an ObjectOutputStream, at least not without effort. There is a trick somewhere about extending ObjectOutputStream and overriding the writeStreamHeader() method so as not to write the stream header the second time, but I'm not in favour of it. You should really rewrite the whole file, maybe as a List.

    2. You don't need all this code. Just make strokes and codes non-static and non-transient, and get rid of the readObject() and writeObject() methods altogether.

    0 讨论(0)
提交回复
热议问题