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


描述

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

声明

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

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

参数

cbuf - 要推回的字符数组。

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

len - 要回退的字符数。

返回值

此方法不返回值。

异常

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, 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