Java.io.Reader.mark() 方法
描述
java.io.Reader.mark(int readAheadLimit) 方法标记流中的当前位置。 对 reset() 的后续调用将尝试将流重新定位到该点。
声明
以下是 java.io.Reader.mark() 方法的声明。
public void mark(int readAheadLimit)
参数
readAheadLimit − 在保留标记的同时限制可以读取的字符数。 读取这么多字符后,尝试重置流可能会失败。
返回值
此方法不返回值。
异常
IOException − 如果流不支持 mark(),或者出现其他 I/O 错误
示例
下面的例子展示了 java.io.Reader.mark() 方法的使用。
package com.tutorialspoint;
import java.io.*;
public class ReaderDemo {
public static void main(String[] args) {
try {
String s = "Hello World";
// create a new StringReader
Reader reader = new StringReader(s);
// read the first five chars
for (int i = 0; i < 5; i++) {
char c = (char) reader.read();
System.out.print("" + c);
}
// mark current position for maximum of 10 characters
reader.mark(10);
// read five more chars
for (int i = 0; i < 6; i++) {
char c = (char) reader.read();
System.out.print("" + c);
}
// reset back to the marked position
reader.reset();
// change line
System.out.println();
// read six more chars
for (int i = 0; i < 6; i++) {
char c = (char) reader.read();
System.out.print("" + c);
}
// close the stream
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Hello World World

