ObjectInputStream available() method doesn't work as expected (Java)

后端 未结 4 1028
时光说笑
时光说笑 2021-01-07 11:00

I\'ve been trying to figure out why a method I\'ve written to read objects from a file didn\'t work and realized that the available() method of ObjectInputStream gave 0 even

4条回答
  •  不知归路
    2021-01-07 11:18

    I found some trick! If you still want to use .available() to read all objects to the end, you can add an integer (ex: out.writeInt(0)) before adding each Object (ex: out.writeObject(obj)) when you write to the file and also read integer before reading each Object. So .available() can read byte left on file and won't be crash! Hope it helps you!

提交回复
热议问题