Java.io.PushbackReader.unread() 方法
描述
java.io.PushbackReader.unread(char[] cbuf) 方法通过将字符数组复制到推回缓冲区的前面来推回它。 此方法返回后,要读取的下一个字符将具有值 cbuf[0],之后的字符将具有值 cbuf[1],依此类推。
声明
以下是 java.io.PushbackReader.unread() 方法的声明。
public void unread(char[] cbuf)
参数
cbuf − 回推字符数组。
返回值
此方法不返回值。
异常
IOException − 如果推回缓冲区空间不足,或者发生其他 I/O 错误。
示例
下面的例子展示了 java.io.PushbackReader.unread() 方法的使用。
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);
try {
// read the first five chars
for (int i = 0; i < 5; i++) {
char c = (char) pr.read();
System.out.print("" + c);
}
// change line
System.out.println();
// create a new array to unread
char cbuf[] = {'w', 'o', 'r', 'l', 'd'};
// unread into cbuf
pr.unread(cbuf);
// read five chars, which is what we unread from cbuf
for (int i = 0; i < 5; i++) {
char c = (char) pr.read();
System.out.print("" + c);
}
// close the stream
pr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Hello world

