Java中的 CharBuffer read() 方法及示例
Java.nio.CharBuffer 类的read()方法用于将字符读入指定的字符缓冲区。缓冲区按原样用作字符的存储库:所做的唯一更改是 put 操作的结果。不执行缓冲区的翻转或倒带。
句法:
public int read(CharBuffer target)
参数:此方法将缓冲区读取字符。
返回值:此方法返回添加到缓冲区的字符数,如果此字符源位于其末尾,则返回 -1。
异常:此方法引发以下异常:-
- IOException – 如果发生 I/O 错误
- NullPointerException – 如果目标为空
- ReadOnlyBufferException – 如果目标是只读缓冲区
下面是说明 read() 方法的示例:
示例 1:
// Java program to demonstrate
// read() method
import java.nio.*;
import java.util.*;
import java.io.IOException;
public class GFG {
public static void main(String[] args)
{
try {
// Declare and initialize the char array
char[] cb1 = { 'x', 'y', 'z' };
char[] cb2 = { 'a', 'b', 'c', 'd', 'e' };
// wrap the char array into CharBuffer
// using wrap() method
CharBuffer charBuffer1
= CharBuffer.wrap(cb1);
// wrap the char array into CharBuffer
// using wrap() method
CharBuffer charBuffer2
= CharBuffer.wrap(cb2);
// print the byte buffer
System.out.println("CharBuffer Before operation is: "
+ Arrays.toString(
charBuffer1.array())
+ "\nTarget Charbuffer: "
+ Arrays.toString(
charBuffer2.array()));
// Get the value of the number of Character
// read from the charBuffer
// using read() method
int value
= charBuffer1
.read(charBuffer2);
// print the byte buffer
System.out.println("\nCharBuffer After operation is: "
+ Arrays.toString(
charBuffer1.array())
+ "\nTarget Charbuffer: "
+ Arrays.toString(
charBuffer2.array())
+ "\nno of value changed: "
+ value);
}
catch (IOException e) {
System.out.println("an I/O error occurs");
System.out.println("Exception throws: " + e);
}
catch (NullPointerException e) {
System.out.println("target charbuffer is null");
System.out.println("Exception throws: " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("target is a read only buffer");
System.out.println("Exception throws: " + e);
}
}
}
输出:
CharBuffer Before operation is: [x, y, z]
Target Charbuffer: [a, b, c, d, e]
CharBuffer After operation is: [x, y, z]
Target Charbuffer: [x, y, z, d, e]
no of value changed: 3
示例 2:对于 NullPointerException
// Java program to demonstrate
// read() method
import java.nio.*;
import java.util.*;
import java.io.IOException;
public class GFG {
public static void main(String[] args)
{
try {
// Declare and initialize the char array
char[] cb1 = { 'x', 'y', 'z' };
// wrap the char array into CharBuffer
// using wrap() method
CharBuffer charBuffer1
= CharBuffer.wrap(cb1);
// print the byte buffer
System.out.println("CharBuffer Before operation is: "
+ Arrays.toString(
charBuffer1.array()));
// Get the value of number of Character
// read from the charBuffer
// using read() method
int value = charBuffer1.read(null);
}
catch (IOException e) {
System.out.println("\nan I/O error occurs");
System.out.println("Exception throws: " + e);
}
catch (NullPointerException e) {
System.out.println("\ntarget charbuffer is null");
System.out.println("Exception throws: " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("\ntarget is a read only buffer");
System.out.println("Exception throws: " + e);
}
}
}
输出:
CharBuffer Before operation is: [x, y, z]
target charbuffer is null
Exception throws: java.lang.NullPointerException
示例 3:对于 ReadOnlyBufferException
// Java program to demonstrate
// read() method
import java.nio.*;
import java.util.*;
import java.io.IOException;
public class GFG {
public static void main(String[] args)
{
try {
// Declare and initialize the char array
char[] cb1 = { 'x', 'y', 'z' };
char[] cb2 = { 'a', 'b', 'c', 'd', 'e' };
// wrap the char array into CharBuffer
// using wrap() method
CharBuffer charBuffer1
= CharBuffer.wrap(cb1);
// wrap the char array into CharBuffer
// using wrap() method
CharBuffer charBuffer2
= CharBuffer.wrap(cb2);
// print the byte buffer
System.out.println("CharBuffer Before operation is: "
+ Arrays.toString(
charBuffer1.array())
+ "\nTarget Charbuffer: "
+ Arrays.toString(
charBuffer2.array()));
// converting Charbuffer to readonlybuff
CharBuffer readonlybuff
= charBuffer2.asReadOnlyBuffer();
// Get the value of number of Character
// read from the charBuffer
// using read() method
int value = charBuffer1.read(readonlybuff);
// print the byte buffer
System.out.println("\nCharBuffer After operation is: "
+ Arrays.toString(
charBuffer1.array())
+ "\nTarget Charbuffer: "
+ Arrays.toString(
charBuffer2.array())
+ "\nno of value changed: "
+ value);
}
catch (IOException e) {
System.out.println("\nan I/O error occurs");
System.out.println("Exception throws: " + e);
}
catch (NullPointerException e) {
System.out.println("\ntarget charbuffer is null");
System.out.println("Exception throws: " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("\ntarget is a read only buffer");
System.out.println("Exception throws: " + e);
}
}
}
输出:
CharBuffer Before operation is: [x, y, z]
Target Charbuffer: [a, b, c, d, e]
target is a read only buffer
Exception throws: java.nio.ReadOnlyBufferException
参考: https://docs.oracle.com/javase/9/docs/api/ Java/nio/CharBuffer.html#read-java.nio.CharBuffer-