Java 中的 CharBuffer get() 方法
java 8programmingobject oriented programming更新于 2024/10/20 9:28:00
使用 java.nio.CharBuffer 类中的 get() 方法读取缓冲区当前位置的值,然后将其递增。此方法返回当前缓冲区位置的值。此外,如果发生下溢情况,则会抛出 BufferUnderflowException。
下面给出了一个演示此操作的程序 −
示例
import java.nio.*; import java.util.*; public class Demo { public static void main(String[] args) { int n = 5; try { CharBuffer buffer = CharBuffer.allocate(n); buffer.put('A'); buffer.put('P'); buffer.put('P'); buffer.put('L'); buffer.put('E'); buffer.rewind(); System.out.println("The CharBuffer is: " + Arrays.toString(buffer.array())); char val1 = buffer.get(); System.out.println("
The value at current position of CharBuffer is: " + val1); char val2 = buffer.get(); System.out.println("The value at next position of CharBuffer is: " + val2); } catch (IllegalArgumentException e) { System.out.println("Error!!! IllegalArgumentException"); } catch (ReadOnlyBufferException e) { System.out.println("Error!!! ReadOnlyBufferException"); } catch (BufferUnderflowException e) { System.out.println("Error!!! BufferUnderflowException"); } } }
输出
The CharBuffer is: [A, P, P, L, E] The value at current position of CharBuffer is: A The value at next position of CharBuffer is: P