天天看點

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