Java Nio 中 java.nio.Buffer 类 读写

1. 三个主要的名词:

  • mark
  • position
  • limit

2. 它们的值范围

0 <= mark <= position <= limit <= length

3. 三个主要方法

  • rewind()

    (mark =-1; position =0;)

  • flip()

    (mark =-1; limit=position; position=0; )

  • clear()

(mark =-1; limit=length; position =0;)

4. 样例

代码:


   ByteBuffer old = ByteBuffer.allocate(8);
    old.putInt(99);
    ByteBuffer newBuf = ByteBuffer.allocate(16);
    old.flip();
    newBuf.put(old);
    newBuf.putInt(100);

    newBuf.flip();
    System.out.println(newBuf.remaining());
    System.out.println(newBuf.getInt());
    System.out.println(newBuf.getInt());

控制台输出:

8
99
100

你可能感兴趣的:(Java Nio 中 java.nio.Buffer 类 读写)