Java.io.PushbackReader.unread() 方法



描述

java.io.PushbackReader.unread(int c) 方法將單個字元推回到回退緩衝區的前面。此方法返回後,下一個將要讀取的字元的值將為 (char)c。

宣告

以下是java.io.PushbackReader.unread() 方法的宣告。

public void unread(int c)

引數

c − 表示要回退的字元的 int 值。

返回值

此方法不返回值。

異常

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();

         // unread a character
         pr.unread('F');

         // read the next char, which is the one we unread
         char c = (char) pr.read();

         // print that character
         System.out.println("" + c);

         // close the stream
         pr.close();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

Hello
F
java_io_pushbackreader.htm
廣告
© . All rights reserved.