📜  Java中的 CharBuffer append() 方法及示例

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

Java中的 CharBuffer append() 方法及示例

附加(字符 c)

Java.nio.CharBuffer类的append(char c)方法用于将指定的字符追加到这个缓冲区(可选操作)。
dst.append(c) 形式的此方法的调用与调用的行为方式完全相同
dst.put(c)
句法 :

public CharBuffer append(char c)

参数:此方法采用 16 位字符追加。
返回值:此方法返回此缓冲区,其中插入了 char 值。
异常:此方法抛出以下异常:

  • BufferOverflowException - 如果此缓冲区中没有足够的空间
  • ReadOnlyBufferException-如果此缓冲区是只读的

以下是说明 append(char c) 方法的示例:
示例 1:

Java
// 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);
        }
    }
}


Java
// 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);
        }
    }
}


Java
// 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);
        }
    }
}


Java
// 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);
        }
    }
}


Java
// 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);
        }
    }
}


Java
// 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);
        }
    }
}


输出:
Original CharBuffer:  [a, b, c]

示例 2:演示 BufferOverflowException。

Java

// 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

// 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-

追加(字符序列 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

// 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

// 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

// 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-