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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。