📌  相关文章
📜  Java中的 ByteBuffer getFloat() 方法及示例

📅  最后修改于: 2022-05-13 01:54:26.532000             🧑  作者: Mango

Java中的 ByteBuffer getFloat() 方法及示例

获取浮动()

Java.nio.ByteBuffer类的getFloat()方法用于读取该缓冲区当前位置接下来的四个字节,按照当前字节顺序将它们组合成一个浮点值,然后将该位置加四。

句法:

public abstract float getFloat()

返回值:此方法返回缓冲区当前位置的浮点值。

异常:如果此缓冲区中剩余的字节数少于四个,则此方法将引发BufferUnderflowException

下面是说明 getFloat() 方法的示例:

示例 1:

// Java program to demonstrate
// getFloat() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 8;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the double value in the bytebuffer
            bb.asFloatBuffer()
                .put(12.3f)
                .put(28.44f);
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // print the ByteBuffer
            System.out.println("Original ByteBuffer: ");
            for (int i = 1; i <= capacity / 4; i++)
                System.out.print(bb.getFloat() + " ");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Reads the Float at this buffer's current position
            // using getFloat() method
            float value = bb.getFloat();
  
            // print the float value
            System.out.println("\n\nByte Value: " + value);
  
            // Reads the  float at this buffer's next position
            // using getFloat() method
            float value1 = bb.getFloat();
  
            // print the float value
            System.out.print("\nNext Byte Value: " + value1);
        }
  
        catch (BufferUnderflowException e) {
  
            System.out.println("\nException Thrown : " + e);
        }
    }
}
输出:
Original ByteBuffer: 
12.3 28.44 

Byte Value: 12.3

Next Byte Value: 28.44

示例 2:

// Java program to demonstrate
// getFloat() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 8;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the double value in the bytebuffer
            bb.asFloatBuffer()
                .put(12.3f)
                .put(28.44f);
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // print the ByteBuffer
            System.out.println("Original ByteBuffer: ");
            for (int i = 1; i <= capacity / 4; i++)
                System.out.print(bb.getFloat() + " ");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Reads the Float at this buffer's current position
            // using getFloat() method
            float value = bb.getFloat();
  
            // print the float value
            System.out.println("\n\nByte Value: " + value);
  
            // Reads the  float at this buffer's next position
            // using getFloat() method
            float value1 = bb.getFloat();
  
            // print the float value
            System.out.println("\nNext Byte Value: " + value1);
  
            // Reads the  float at this buffer's next position
            // using getFloat() method
            float value2 = bb.getFloat();
        }
  
        catch (BufferUnderflowException e) {
            System.out.println("\nthere are fewer "
                               + "than eight bytes remaining"
                               + " in this buffer");
            System.out.println("Exception Thrown : " + e);
        }
    }
}
输出:
Original ByteBuffer: 
12.3 28.44 

Byte Value: 12.3

Next Byte Value: 28.44

there are fewer than eight bytes remaining in this buffer
Exception Thrown : java.nio.BufferUnderflowException

参考: https://docs.oracle.com/javase/9/docs/api/ Java/nio/ByteBuffer.html#getFloat–

getFloat(整数索引)

Java.nio.ByteBuffergetFloat(int index)方法用于读取给定索引处的四个字节,根据当前字节顺序将它们组合成一个浮点值。

句法:

public abstract float getFloat(int index)

参数:此方法将索引作为参数,该参数是将从中读取字节的索引。

返回值:此方法返回给定索引处的浮点值。

异常:如果 index 为负数或不小于缓冲区的限制,此方法将引发IndexOutOfBoundsException

以下是说明getFloat(int index)方法的示例:

示例 1:

// Java program to demonstrate
// getFloat() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 8;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the double value in the bytebuffer
            bb.asFloatBuffer()
                .put(12.3f)
                .put(28.44f);
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // print the ByteBuffer
            System.out.println("Original ByteBuffer: ");
            for (int i = 1; i <= capacity / 4; i++)
                System.out.print(bb.getFloat() + " ");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Reads the Float at this buffer's current position
            // using getFloat() method
            float value = bb.getFloat(0);
  
            // print the float value
            System.out.println("\n\nByte Value: " + value);
  
            // Reads the  float at this buffer's next position
            // using getFloat() method
            float value1 = bb.getFloat(4);
  
            // print the float value
            System.out.println("\nNext Byte Value: " + value1);
        }
  
        catch (IndexOutOfBoundsException e) {
  
            System.out.println("\nindex is negative or smaller"
                               + " than the buffer's limit, "
                               + "minus seven");
            System.out.println("Exception Thrown : " + e);
        }
    }
}
输出:
Original ByteBuffer: 
12.3 28.44 

Byte Value: 12.3

Next Byte Value: 28.44

示例 2:

// Java program to demonstrate
// getFloat() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the ByteBuffer
        int capacity = 8;
  
        // Creating the ByteBuffer
        try {
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.allocate(capacity);
  
            // putting the double value in the bytebuffer
            bb.asFloatBuffer()
                .put(12.3f)
                .put(28.44f);
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // print the ByteBuffer
            System.out.println("Original ByteBuffer: ");
            for (int i = 1; i <= capacity / 4; i++)
                System.out.print(bb.getFloat() + " ");
  
            // rewind the Bytebuffer
            bb.rewind();
  
            // Reads the Float at this buffer's current position
            // using getFloat() method
            float value = bb.getFloat(0);
  
            // print the float value
            System.out.println("\n\nByte Value: " + value);
  
            // Reads the  float at this buffer's next position
            // using getFloat() method
            float value1 = bb.getFloat(6);
  
            // print the float value
            System.out.println("\nNext Byte Value: " + value1);
        }
  
        catch (IndexOutOfBoundsException e) {
  
            System.out.println("\nindex is negative or"
                               + " smaller than the buffer's "
                               + "limit, minus seven");
            System.out.println("Exception Thrown : " + e);
        }
    }
}
输出:
Original ByteBuffer: 
12.3 28.44 

Byte Value: 12.3

index is negative or smaller than the buffer's limit, minus seven
Exception Thrown : java.lang.IndexOutOfBoundsException

参考: https://docs.oracle.com/javase/9/docs/api/ Java/nio/ByteBuffer.html#getFloat-int-