Getting and Setting Non-Byte Java Types in a ByteBuffer - Java File Path IO

Java examples for File Path IO:ByteBuffer

Description

Getting and Setting Non-Byte Java Types in a ByteBuffer

Demo Code

import java.nio.ByteBuffer;

public class Main {
  public static void main(String[] args) {
    // Obtain a ByteBuffer; see also Creating a ByteBuffer
    ByteBuffer buf = ByteBuffer.allocate(100);

    // Put values of different types
    buf.putChar((char) 123);
    buf.putShort((short) 123);
    buf.putInt(123);//www  .ja  v a  2s  . c  om
    buf.putLong(123L);
    buf.putFloat(12.3F);
    buf.putDouble(12.3D);

    // Reset position for reading
    buf.flip();

    // Retrieve the values
    char c = buf.getChar();
    short s = buf.getShort();
    int i = buf.getInt();
    long l = buf.getLong();
    float f = buf.getFloat();
    double d = buf.getDouble();
  }
}

Related Tutorials