首页javanio_bufferJava I/O - 如何将ByteBuffer转换为CharBuffer

Java I/O - 如何将ByteBuffer转换为CharBuffer

我们想知道如何将ByteBuffer转换为CharBuffer。
       

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

public class MainClass {
  public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' });
    bb.rewind();
    CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer();
    System.out.println("Char Buffer");
    while (cb.hasRemaining())
      System.out.println(cb.position() + " -> " + cb.get());
  }
}