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

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

我们想知道如何将ByteBuffer转换为IntBuffer。
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

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();
    IntBuffer ib = ((ByteBuffer) bb.rewind()).asIntBuffer();
    System.out.println("Int Buffer");
    while (ib.hasRemaining())
      System.out.println(ib.position() + " -> " + ib.get());

  }
}