Why Java OutputStream.write() Takes Integer but Writes Bytes

后端 未结 5 685
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 07:01

I am writing an OutputStream, just noticed this in the OutputStream interface,

   public abstract void write(int b) throws IOException;

Thi

5条回答
  •  旧巷少年郎
    2020-12-05 07:26

    Maybe it's because bytes are signed by default, and files store bytes as unsigned values. That is why read() returns an int - to give 255 instead of -1 for $FF. Same with write(int), you can not store $FF as 255 in a byte.

提交回复
热议问题