当前位置:   article > 正文

Java NIO Buffer类型化put和get读写顺序要一致_nio buffer.putint

nio buffer.putint

ByteBuffer 类型化put和get

放入什么类型,取出就要用什么类型,顺序要一致。否则会报 BufferUnderflowException

测试代码

public static void main(String[] args) {
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    buffer.putInt(100);
    buffer.putLong(100L);
    buffer.putChar('陈');
    buffer.putShort((short) 4);

    //调用buffer.flip() 切换到读模式
    buffer.flip();

    System.out.println(buffer.getInt());
    System.out.println(buffer.getLong());
    System.out.println(buffer.getChar());
    System.out.println(buffer.getShort());
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/1005595
推荐阅读
相关标签
  

闽ICP备14008679号