Java.io.FilterReader.mark() 方法
描述
java.io.FilterReader.mark(int readAheadLimit) 方法标记流的当前位置。
声明
以下是 java.io.FilterReader.mark(int readAheadLimit) 方法的声明 −
public void mark(int readAheadLimit)
参数
readAheadLimit − 在保留标记的同时限制可以读取的字符数。
返回值
此方法不返回任何值。
异常
IOException − 如果发生 I/O 错误。
示例
下面的例子展示了 java.io.FilterReader.mark(int readAheadLimit) 方法的使用。
package com.tutorialspoint;
import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
public class FilterReaderDemo {
public static void main(String[] args) throws Exception {
FilterReader fr = null;
Reader r = null;
try {
// create new reader
r = new StringReader("ABCDEF");
// create new filter reader
fr = new FilterReader(r) {
};
// reads and prints FilterReader
System.out.println((char)fr.read());
System.out.println((char)fr.read());
// mark invoked at this position
fr.mark(0);
System.out.println("mark() invoked");
System.out.println((char)fr.read());
System.out.println((char)fr.read());
// reset() repositioned the stream to the mark
fr.reset();
System.out.println("reset() invoked");
System.out.println((char)fr.read());
System.out.println((char)fr.read());
} catch(IOException e) {
// if any I/O error occurs
e.printStackTrace();
} finally {
// releases system resources associated with this stream
if(r!=null)
r.close();
if(fr!=null)
fr.close();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
A B mark() invoked C D reset() invoked C D

