我该怎么做类似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的字节。所以那样做。