Java.io.PushbackReader.mark() 方法



描述

java.io.PushbackReader.mark(int readAheadLimit) 方法標記流中的當前位置。PushbackReader 類的標記始終丟擲異常。

宣告

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

public void mark(int readAheadLimit)

引數

readAheadLimit - 在仍保留標記的情況下可以讀取的字元數的限制。讀取這麼多字元後,嘗試重置流可能會失敗。

返回值

此方法不返回值。

異常

IOException - 始終,因為不支援標記。

示例

以下示例顯示了 java.io.PushbackReader.mark() 方法的使用。

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

         // mark the current position results in exception
         pr.mark(5);

         // close the stream
         pr.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

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

Hello
java.io.IOException: mark/reset not supported
java_io_pushbackreader.htm
廣告

© . All rights reserved.