Convert ByteBuffer to a FloatBuffer in Java

Description

The following code shows how to convert ByteBuffer to a FloatBuffer.

Example


/*ww w.  jav  a  2 s  .  c  om*/
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

public class Main {
  public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' });
    bb.rewind();
    FloatBuffer fb = ((ByteBuffer) bb.rewind()).asFloatBuffer();
    System.out.println("Float Buffer");
    while (fb.hasRemaining())
      System.out.println(fb.position() + " -> " + fb.get());

  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip