Java的.nio.CharBuffer类在Java中
CharBuffer保存要在 I/O 操作中使用的整数值序列。 CharBuffer类对长缓冲区提供以下四类操作:
- 读取单个字符的绝对和相对获取方法。
- 写入单个字符的绝对和相对放置方法。
- 相对批量 put 和 get 方法将连续的 Chars 序列从 int 数组或其他一些 Chars 缓冲区传输到此缓冲区,并从此缓冲区传输到数组。
可以通过以下方式创建短缓冲区:
- 分配():这为缓冲区的内容分配空间。
- wrap():将现有的长数组包装到缓冲区中。
CharBuffer 类的大部分方法都直接类似于 ByteBuffer 定义的方法。
类声明:
public abstract class CharBuffer
extends Buffer
implements Comparable
CharBuffer 类的方法:Method Description allocate(int capacity) This method allocates a new char buffer. append(char c) This method appends the specified char to this buffer. append(CharSequence csq) This method appends the specified character sequence to this buffer. append(CharSequence csq, int start, int end) This method appends a subsequence of the specified character sequence to this buffer array() This method returns the char array that backs this buffer arrayOffset() This method returns the offset within this buffer’s backing array of the first element of the buffer asReadOnlyBuffer() This method creates a new, read-only char buffer that shares this buffer’s content. charAt(int index) This method reads the character at the given index relative to the current position. compact() This method compacts this buffer compareTo(CharBuffer that) This method compares this buffer to another. duplicate() This method creates a new char buffer that shares this buffer’s content. equals(Object ob) This method tells whether this buffer is equal to another object. get() This method is a relative get method and returns get(char[] dst) This method is a relative bulk get method and returns get(char[] dst, int offset, int length) This method is a relative bulk get method and returns get(int index) This method is an absolute get method and returns hasArray() This method tells whether this buffer is backed by an accessible char array. hashCode() This method returns the current hash code of this buffer. isDirect() This method tells whether this char buffer is direct. length() This method returns the length of this character buffer. order() This method retrieves this buffer’s byte order. put(char c) This method is a relative put method and returns put(char[] src) This method is a relative bulk put method and returns put(char[] src, int offset, int length) This method is a relative bulk put method and returns put(CharBuffer src) This method is a relative bulk put method and returns put(int index, char c) This method is an absolute put method and returns put(String src) This method is a relative bulk put method and returns put(String src, int start, int end) This method is a relative bulk put method and returns read(CharBuffer target) This method attempts to read characters into the specified character buffer. slice() This method creates a new char buffer whose content is a shared subsequence of this buffer’s content. subSequence(int start, int end) This method creates a new character buffer that represents the specified subsequence of this buffer, relative to the current position. toString() This method returns a string containing the characters in this buffer. wrap(char[] array) This method wraps a char array into a buffer. wrap(char[] array, int offset, int length) This method wraps a char array into a buffer. wrap(CharSequence csq) This method wraps a character sequence into a buffer. wrap(CharSequence csq, int start, int end) This method wraps a character sequence into a buffer.
以下是一些演示 CharBuffer 类及其方法的程序:
示例 1:
Java
// Java program to demonstrate
// CharBuffer class
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the CharBuffer
char capacity = 10;
// Creating the CharBuffer
// creating object of Charbuffer
// and allocating size capacity
CharBuffer fb = CharBuffer.allocate(capacity);
// putting the value in charbuffer
fb.put('a');
fb.put(5, 'b');
fb.put(7, 'c');
// Printing the CharBuffer
System.out.println("ChartBuffer: "
+ Arrays.toString(fb.array()));
}
}
Java
// Java program to demonstrate
// CharBuffer class
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the CharBuffer
int capacity = 4;
// 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')
.append('d')
.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);
}
}
}
输出:
ChartBuffer: [a, , , , , b, , c, , ]
示例 2:
Java
// Java program to demonstrate
// CharBuffer class
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the CharBuffer
int capacity = 4;
// 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')
.append('d')
.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, d]