Java.io.StringReader.mark() 方法
描述
java.io.StringReader.mark(int readAheadLimit) 方法标记流中的当前位置。 对 reset() 的后续调用会将流重新定位到该点。
声明
以下是 java.io.StringReader.mark() 方法的声明。
public void mark(int readAheadLimit)
参数
readAheadLimit − 在保留标记的同时限制可以读取的字符数。 因为流的输入来自字符串,没有实际限制,所以这个参数不能为负数,否则会被忽略。
返回值
此方法不返回值。
异常
IllegalArgumentException − 如果 readAheadLimit 是 < 0.
IOException − 如果发生 I/O 错误。
示例
下面的例子展示了 java.io.StringReader.mark() 方法的使用。
package com.tutorialspoint;
import java.io.*;
public class StringReaderDemo {
public static void main(String[] args) {
String s = "Hello World";
// create a new StringReader
StringReader sr = new StringReader(s);
try {
// read the first five chars
for (int i = 0; i < 5; i++) {
char c = (char) sr.read();
System.out.print("" + c);
}
// mark the reader at position 5 for maximum 6
sr.mark(6);
// read the next six chars
for (int i = 0; i < 6; i++) {
char c = (char) sr.read();
System.out.print("" + c);
}
// reset back to marked position
sr.reset();
// read again the next six chars
for (int i = 0; i < 6; i++) {
char c = (char) sr.read();
System.out.print("" + c);
}
// close the stream
sr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Hello World World

