天天看点

java 流 mark_Java CharArrayReader mark()方法

Java CharArrayReader mark()方法

java.io.CharArrayReader.mark(int readAheadLimit) 用于标记流中的当前位置。调用reset()会将流重新定位。

1 语法

public void mark(int readAheadLimit)

2 参数

readAheadLimit:参数设置保留标记时可以读取的字符数限制。由于实际上没有限制,因为流的输入来自字符数组,因此通常会忽略该参数。

3 返回值

4 示例

package com.yiidian;

import java.io.CharArrayReader;

import java.io.IOException;

public class Demo {

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