CharBuffer duplicate() 方法,Java
可以使用 java.nio.CharBuffer 类中的方法 duplicate() 创建一个 Buffer 的副本 buffer。此副本 buffer 与原始 buffer 相同。方法 duplicate() 返回所创建的副本 buffer。
演示此方法的程序如下 −
举例
import java.nio.*; import java.util.*; public class Demo { public static void main(String[] args) { int n = 5; try { CharBuffer buffer1 = CharBuffer.allocate(5); buffer1.put('A'); buffer1.put('P'); buffer1.put('P'); buffer1.put('L'); buffer1.put('E'); buffer1.rewind(); System.out.println("The Original CharBuffer is: " + Arrays.toString(buffer1.array())); CharBuffer buffer2 = buffer1.duplicate(); System.out.print("The Duplicate CharBuffer is: " + Arrays.toString(buffer2.array())); } catch (IllegalArgumentException e) { System.out.println("Error!!! IllegalArgumentException"); } catch (ReadOnlyBufferException e) { System.out.println("Error!!! ReadOnlyBufferException"); } } }
输出
The Original CharBuffer is: [A, P, P, L, E] The Duplicate CharBuffer is: [A, P, P, L, E]
广告