首页javanio_bufferJava I/O - 如何使用while循环来读取字节

Java I/O - 如何使用while循环来读取字节

我们想知道如何使用while循环来读取字节。
import java.nio.ByteBuffer;

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();
    System.out.println("Byte Buffer");
    while (bb.hasRemaining())
      System.out.println(bb.position() + " -> " + bb.get());
  }
}