Java Nio 中 java.nio.Buffer 類 讀寫

###1. 三個主要的名詞:###java

  • mark
  • position
  • limit

###2. 它們的值範圍###code

0 <= mark <= position <= limit <= lengthip

###3. 三個主要方法###rem

  • rewind()

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

  • flip()

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

  • clear()

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

###4. 樣例### 代碼:方法

<!-- lang: java -->
   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());

控制檯輸出:im

8 99 100co

相關文章
相關標籤/搜索