CharBuffer 类提供一个字符(char)序列缓冲区。由于之前大量的API已经在ByteBuffer里面讲过了,这里不做过多的赘述。
public CharBuffer append(char c)方法的作用:将指定字符添加到此缓冲区(可选操作)。调用此方法的形式为dst.append©,该调用与以下调用完全相同dst.put©。
public CharBuffer append(CharSequence csq)方法的作用,将指定的字符序列添加到此缓冲区。调用此方法的形式为dst.append(csq),该调用与以下调用完全相同:dst.put(csq.tosString()),有可能没有添加整个序列,这取决于针对字符序列csq的toString规范。例如,调用字符缓冲区的toString()方法将返回一个子序列,其内容取决于缓冲区的位置和限制。
public CharBuffer append(CharSequence csq , int start , int end)方法作用:将指定字符序列的子序列添加到此缓冲区。当csq不为null的时候,调用此方法的形式为dst.append(csq,start,end),调用与以下调用完全相同:dst.put(csq.subSequence(start,end).toString)。
示例代码:
public class AppendDemo {
public static void main(String[] args) {
CharBuffer buffer = CharBuffer.allocate(15);
System.out.println("A position = "+buffer.position());
buffer.append('a');
System.out.println("B position = "+buffer.position());
buffer.append("bcdef");
System.out.println("C position = "+buffer.position());
buffer.append("abcuhwief",3,8);
System.out.println("D position = "+buffer.position());
System.out.println(Arrays.toString(buffer.array()));
System.out.println("E capacity = "+buffer.capacity());
}
}
public final char charAt(int index)方法的作用:读取相对于当前位置的给定索引处的字符:
示例代码:
public class CharAtDemo {
public static void main(String[] args) {
CharBuffer buffer = CharBuffer.allocate(15);
buffer.append("auegwvl");
buffer.position(2);
System.out.println(buffer.charAt(0));
System.out.println(buffer.charAt(1));
System.out.println(buffer.charAt(2));
}
}
示例代码:
public class PrsDemo {
public static void main(String[] args) throws IOException {
CharBuffer buffer = CharBuffer.allocate(8);
buffer.append("av123456");
buffer.position(2);
buffer.put("cde");
buffer.rewind();
System.out.println(Arrays.toString(buffer.array()));
buffer.position(1);
CharBuffer buffer2 = CharBuffer.allocate(4);
System.out.println("buffer2 position = "+buffer2.position());
buffer.read(buffer2);
System.out.println("buffer2 position = "+buffer2.position());
buffer2.rewind();
System.out.println(Arrays.toString(buffer2.array()));
System.out.println(Arrays.toString(buffer.array()));
buffer.position(2);
CharBuffer buffer3 = buffer.subSequence(0, 2);
System.out.println("buffer3 capacity = " + buffer3.capacity() +
" limit="+buffer3.limit()+" position="+buffer3.position());
System.out.println(Arrays.toString(buffer3.array()));
}
}
static CharBuffer wrap(CharSequence csq , int start , int end)方法的作用:将字符序列包装到缓冲区中。新的只读缓冲区的内容将为给定字符序列的内容。缓冲区的容量将为csq.length,其位置将为start,限制为end,标记未定义。参数解释:
1. 参数csq代表字符序列,新的字符缓冲区将从中创建
2. 参数start代表第一个要使用的字符索引,必须为非负且不大于csq.length,新缓冲区的位置将被设置为start
3. 参数end代表最后一个要使用的字符索引,必须不小于start且不大于csq.length,新缓冲区的位置将被设置为end
示例代码:
public class CharBufferWrapDemo {
public static void main(String[] args) {
CharBuffer charBuffer = CharBuffer.wrap("abcdefg", 3, 5);
System.out.println("charBuffer capacity = " + charBuffer.capacity() +
" limit="+charBuffer.limit()+" position="+charBuffer.position());
for (int i = 0; i < charBuffer.limit(); i++) {
System.out.print(charBuffer.get(i)+" ");
}
charBuffer.append("我是只读的,不能添加数据,会出现异常");
}
}
public final int length()方法的作用:返回此缓冲区的长度。当字符型缓冲区视为字符序列时,长度只是该位置和限制之间的字符数,即长度等效于remaining()
示例代码:
public class LengthDemo {
public static void main(String[] args) {
CharBuffer buffer = CharBuffer.wrap("abcd");
System.out.println("position = " + buffer.position() +
" remaining = " + buffer.remaining() +
" length = " + buffer.length());
System.out.println(buffer.get());
System.out.println("position = " + buffer.position() +
" remaining = " + buffer.remaining() +
" length = " + buffer.length());
System.out.println(buffer.get());
System.out.println("position = " + buffer.position() +
" remaining = " + buffer.remaining() +
" length = " + buffer.length());
System.out.println(buffer.get());
System.out.println("position = " + buffer.position() +
" remaining = " + buffer.remaining() +
"length = " + buffer.length());
System.out.println(buffer.get());
System.out.println("position = " + buffer.position() +
" remaining = " + buffer.remaining() +
" length = " + buffer.length());
}
}