目录

void unread(char[] cbuf, int off, int len)

描述 (Description)

java.io.PushbackReader.unread(char[] cbuf,int off,int len)方法通过将字符数组的一部分复制到回送缓冲区的前面来推回它。 返回此方法后,下一个要读取的字符将具有值cbuf [off],之后的字符将具有值cbuf [off + 1],依此类推。

声明 (Declaration)

以下是java.io.PushbackReader.unread()方法的声明。

public void unread(char[] cbuf,int off,int len)

参数 (Parameters)

  • cbuf - 要推回的字符数组。

  • off - 第一个字符的偏移量。

  • len - 要回退的字符数。

返回值 (Return Value)

此方法不返回值。

异常 (Exception)

IOException - 如果回送缓冲区中没有足够的空间,或者发生其他I/O错误。

例子 (Example)

以下示例显示了java.io.PushbackReader.unread()方法的用法。

package com.iowiki;
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, 1, 4);
         // read 4 chars, which is what we unread from cbuf
         for (int i = 0; i < 4; i++) {
            char c = (char) pr.read();
            System.out.print("" + c);
         }
         // close the stream
         pr.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

Hello
orld
↑回到顶部↑
WIKI教程 @2018