Java CharBuffer append()方法及实例
append(char c)
java.nio.CharBuffer 类的 append(char c) 方法用于将指定的char追加到这个缓冲区(可选操作)。
调用这个方法的形式dst.append(c)的行为与调用
dst.put(c)的行为完全相同
语法:
public CharBuffer append(char c)
参数: 该方法接受要追加的16位字符。
返回值: 该方法返回这个缓冲区,其中插入了字符值。
异常: 该方法抛出以下异常。
- BufferOverflowException- 如果这个缓冲区没有足够的空间。
- ReadOnlyBufferException- 如果这个缓冲区是只读的。
下面是说明append(char c)方法的例子:
例1 :
// Java program to demonstrate
// append() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the CharBuffer
int capacity = 3;
// Creating the CharBuffer
try {
// creating object of CharBuffer
// and allocating size capacity
CharBuffer charbuffer
= CharBuffer.allocate(capacity);
// append the value in CharBuffer
// using append() method
charbuffer.append('a')
.append('b')
.append('c')
.rewind();
// print the CharBuffer
System.out.println("Original CharBuffer: "
+ Arrays.toString(
charbuffer.array()));
}
catch (BufferOverflowException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出
Original CharBuffer: [a, b, c]
例2: 为了证明BufferOverflowException。
// Java program to demonstrate
// append() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the CharBuffer
int capacity = 3;
// Creating the CharBuffer
try {
// creating object of CharBuffer
// and allocating size capacity
CharBuffer charbuffer
= CharBuffer.allocate(capacity);
// append the value in CharBuffer
// using append() method
charbuffer.append('a')
.append('b')
.append('c');
// print the CharBuffer
System.out.println("Original CharBuffer: "
+ Arrays.toString(
charbuffer.array()));
// again appending the value in CharBuffer
// using append() method
System.out.println("\nBuffer position : "
+ charbuffer.position());
charbuffer.append('x');
}
catch (BufferOverflowException e) {
System.out.println("buffer's current position "
+ "is not smaller than its limit");
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出
Original CharBuffer: [a, b, c]
Buffer position : 3
buffer's current position is not smaller than its limit
Exception throws : java.nio.BufferOverflowException
实例3: 为了证明ReadOnlyBufferException。
// Java program to demonstrate
// append() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the CharBuffer
int capacity = 3;
// Creating the CharBuffer
try {
// creating object of CharBuffer
// and allocating size capacity
CharBuffer charbuffer
= CharBuffer.allocate(capacity);
// append the value in CharBuffer
// using append() method
charbuffer.append('a')
.append('b')
.append('c');
// print the CharBuffer
System.out.println("Original CharBuffer: "
+ Arrays.toString(
charbuffer.array()));
// Creating a read-only copy of CharBufferBuffer
// using asReadOnlyBuffer() method
CharBuffer chb = charbuffer.asReadOnlyBuffer();
System.out.println("\nTrying to append the char value"
+ " in read-only buffer");
// putting the value in readonly CharBuffer
// using append() method
chb.append('d');
}
catch (BufferOverflowException e) {
System.out.println("buffer's current position "
+ "is not smaller than its limit");
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出
Original CharBuffer: [a, b, c]
Trying to append the char value in read-only buffer
Exception throws : java.nio.ReadOnlyBufferException
参考资料 :https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#append-char-
append(CharSequence csq)
java.nio.CharBuffer类 的 append(CharSequence csq) 方法用于将指定的字符序列追加到这个缓冲区(可选操作)。
根据对字符序列csq的toString指定,整个序列可能不会被追加。例如,调用一个字符缓冲区的toString方法将返回一个子序列,其内容取决于缓冲区的位置和限制。
语法:
public CharBuffer append(CharSequence csq)
参数: 本方法接受要追加的 字符序列 。如果csq是空的,那么四个字符 “null “将被追加到这个字符缓冲区。
返回值: 该方法返回这个缓冲区。
异常: 该方法抛出以下异常:
- BufferOverflowException- 如果这个缓冲区没有足够的空间
- ReadOnlyBufferException- 如果这个缓冲区是只读的。
下面是说明append(CharSequence csq)方法的例子:
例1:
// Java program to demonstrate
// append() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the CharBuffer
int capacity = 3;
// Declaring and initializing CharSequence
CharSequence cha = "cow";
// Creating the CharBuffer
try {
// creating object of CharBuffer
// and allocating size capacity
CharBuffer charbuffer
= CharBuffer.allocate(capacity);
// appending the CharSequence in CharBuffer
// using append() method
charbuffer.append(cha)
.rewind();
// print the CharBuffer
System.out.println("Original CharBuffer: "
+ Arrays.toString(
charbuffer.array()));
}
catch (BufferOverflowException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出
Original CharBuffer:
例2: 为了证明BufferOverflowException。
// Java program to demonstrate
// append() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the CharBuffer
int capacity = 2;
// Declaring and initializing CharSequence
CharSequence cha = "cow";
// Creating the CharBuffer
try {
// creating object of CharBuffer
// and allocating size capacity
CharBuffer charbuffer
= CharBuffer.allocate(capacity);
// appending the CharSequence in CharBuffer
// using append() method
charbuffer.append(cha)
.rewind();
// print the CharBuffer
System.out.println("Original CharBuffer: "
+ Arrays.toString(
charbuffer.array()));
}
catch (BufferOverflowException e) {
System.out.println("CharSequence length is greater"
+ " than the length of charbuffer");
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出
CharSequence length is greater than the length of charbuffer
Exception throws : java.nio.BufferOverflowException
例3: 为了演示ReadOnlyBufferException。
// Java program to demonstrate
// append() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the CharBuffer
int capacity = 2;
// Declaring and initializing CharSequence
CharSequence cha = "cow";
// Creating the CharBuffer
try {
// creating object of CharBuffer
// and allocating size capacity
CharBuffer charbuffer
= CharBuffer.allocate(capacity);
// Creating a read-only copy of CharBuffer
// using asReadOnlyBuffer() method
CharBuffer charbuffer1
= charbuffer.asReadOnlyBuffer();
// appending the CharSequence in CharBuffer
// using append() method
charbuffer1.append(cha)
.rewind();
// print the CharBuffer
System.out.println("Original CharBuffer: "
+ Arrays.toString(
charbuffer1.array()));
}
catch (BufferOverflowException e) {
System.out.println("CharSequence length is greater"
+ " than the length of charbuffer");
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Trying to append CharSequence "
+ "in read-only charbuffer");
System.out.println("Exception throws : " + e);
}
}
}
输出
Trying to append CharSequence in read-only charbuffer
Exception throws : java.nio.ReadOnlyBufferException
参考资料: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#append-java.lang.CharSequence-