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.yiibai; 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