append(char c)
java.nio.CharBuffer类的append(char c)方法用于将指定的char附加到此缓冲区(可选操作)。调用dst.append(c)形式的方法的行为与调用dst.put(c)的行为完全相同
用法:
public CharBuffer append(char c)
参数:此方法需要附加16位char。
返回值:此方法返回此缓冲区,在其中插入char值。
异常:此方法引发以下异常:
- 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,则将四个字符“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 intializing 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 intializing 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 intializing 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-
相关用法
- Java CharBuffer order()用法及代码示例
- Java CharBuffer position()用法及代码示例
- Java CharBuffer clear()用法及代码示例
- Java CharBuffer flip()用法及代码示例
- Java CharBuffer chars()用法及代码示例
- Java CharBuffer limit()用法及代码示例
- Java CharBuffer charAt()用法及代码示例
- Java CharBuffer mark()用法及代码示例
- Java CharBuffer length()用法及代码示例
- Java CharBuffer rewind()用法及代码示例
- Java CharBuffer subSequence()用法及代码示例
- Java CharBuffer read()用法及代码示例
- Java CharBuffer reset()用法及代码示例
- Java CharBuffer put()用法及代码示例
- Java CharBuffer get()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 CharBuffer append() methods in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。