我該怎麼做類似ByteBuffer.putBoolean和ByteBuffer.getBoolean?ByteBuffer的等價DataOutputStream.readBoolean和DataOutputStream.writeBoolean?
與DataInputStream所有readBoolean並用DataOutputStream類有writeBoolean;我沒有發現用ByteBuffer寫/讀布爾值的能力。有沒有辦法達到同樣的行爲?
我該怎麼做類似ByteBuffer.putBoolean和ByteBuffer.getBoolean?ByteBuffer的等價DataOutputStream.readBoolean和DataOutputStream.writeBoolean?
與DataInputStream所有readBoolean並用DataOutputStream類有writeBoolean;我沒有發現用ByteBuffer寫/讀布爾值的能力。有沒有辦法達到同樣的行爲?
你可以做類似
boolean b = buf.get() == 1;
buf.put((byte) (b ? 1 : 0));
查看Javadoc。 writeBoolean()根據參數是true還是false寫入一個1或0的字節。所以那樣做。