Java I/O How to - Create ByteBuffer from byte array








Question

We would like to know how to create ByteBuffer from byte array.

Answer

import java.nio.ByteBuffer;
// w  w  w .  j a  va 2s  . c om
public class Main {
  public static void main(String[] argv) throws Exception {

    
    byte[] bytes = new byte[10];
    ByteBuffer buf = ByteBuffer.wrap(bytes);
  }
}