Java.io.CharArrayReader.mark() 方法



描述

java.io.CharArrayReader.mark(int readAheadLimit) 方法标记流中的当前位置。调用 reset() 将使流重新定位到此点。

声明

以下是 java.io.CharArrayReader.mark(int readAheadLimit) 方法的声明:

public void mark(int readAheadLimit)

参数

readAheadLimit - 该参数设置在保留标记时可以读取的字符数的限制。由于流的输入来自字符数组,因此该参数通常被忽略。

返回值

该方法不返回值。

异常

IOException - 如果发生 I/O 错误。

示例

以下示例演示了 java.io.CharArrayReader.mark(int readAheadLimit) 方法的用法。

package com.tutorialspoint;

import java.io.CharArrayReader;
import java.io.IOException;

public class CharArrayReaderDemo {
   public static void main(String[] args) {      CharArrayReader car = null;
      char[] ch = {'A', 'B', 'C', 'D', 'E'};

      try {
         // create new character array reader
         car = new CharArrayReader(ch);
         
         // read and print the characters from the stream
         System.out.println(car.read());
         System.out.println(car.read());
         
         // mark() is invoked at this position
         car.mark(0);
         System.out.println("Mark() is invoked");
         System.out.println(car.read());
         System.out.println(car.read());
         
         // reset() is invoked at this position
         car.reset();
         System.out.println("Reset() is invoked");
         System.out.println(car.read());
         System.out.println(car.read());
         System.out.println(car.read());
         
      } catch(IOException e) {
         // if I/O error occurs
         System.out.print("Stream is already closed");
      } finally {
         // releases any system resources associated with the stream
         if(car!=null)
            car.close();
      }
   }
}

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

65
66
Mark() is invoked
67
68
Reset() is invoked
67
68
69
java_io_chararrayreader.htm
广告

© . All rights reserved.