Java.io.PushbackReader.read() 方法
描述
java.io.PushbackReader.read(char[] cbuf,int off,int len) 方法将字符读入数组的一部分。
声明
以下是 java.io.PushbackReader.read() 方法的声明。
public int read(char[] cbuf,int off,int len)
参数
cbuf − 目标缓冲区。
off − 开始写入字符的偏移量。
len − 要读取的最大字符数。
返回值
此方法返回读取的字符数,如果已到达流的末尾,则返回 -1。
异常
IOException − 如果发生 I/O 错误
示例
下面的例子展示了 java.io.PushbackReader.read() 方法的使用。
package com.tutorialspoint;
import java.io.*;
public class PushbackReaderDemo {
public static void main(String[] args) {
String s = "Hello World";
// create a new StringReader
StringReader sr = new StringReader(s);
// create a new PushBack reader based on our string reader
PushbackReader pr = new PushbackReader(sr, 20);
// create a char array to read chars into
char cbuf[] = new char[5];
try {
// read characters into an array.
System.out.println("" + pr.read(cbuf));
// print cbuf
System.out.println(cbuf);
// Close the stream
pr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
5 Hello

