Java.io.CharArrayReader.read() 方法
描述
java.io.CharArrayReader.read(char[] b, int off, int len) 方法将字符读入指定数组的一部分。
声明
以下是 java.io.CharArrayReader.read(char[] b, int off, int len) 方法的声明 −
public int read(char[] b, int off, int len)
参数
b − 目标数组。
off − 开始存储字符的偏移量。
len − 要读取的字符数。
返回值
实际读取的字符数,-1 表示流结束。
异常
IOException − 如果发生任何 I/O 错误。
示例
下面的例子展示了 java.io.CharArrayReader.read(char[] b, int off, int len) 方法的使用。
package com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) { CharArrayReader car = null;
char[] ch = {'H', 'E', 'L', 'L', 'O'};
char[] d = new char[5];
try {
// create new character array reader
car = new CharArrayReader(ch);
// read character to the destination buffer
car.read(d, 3, 2);
// for every character in the buffer
for (char c : d) {
int i = (int)c;
if(i == 0) {
System.out.println("0");
} else {
System.out.println(c);
}
}
} catch(IOException e) {
// if I/O error occurs
System.out.print("Stream is already closed");
} finally {
// releases any system resources associated with the stream
if(car!=null)
car.close();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
0 0 0 H E

