Java.io.Reader.reset() 方法
描述
java.io.Reader.reset() 方法重置流。 如果流已被标记,则尝试将其重新定位在标记处。 如果流尚未被标记,则尝试以适合特定流的某种方式将其重置,例如将其重新定位到其起点。
声明
以下是 java.io.Reader.reset() 方法的声明。
public void reset()
参数
NA
返回值
此方法不返回值。
异常
IOException − 如果流尚未被标记,或者标记已失效,或者流不支持 reset(),或者发生其他一些 I/O 错误。
示例
下面的例子展示了 java.io.Reader.reset() 方法的使用。
package com.tutorialspoint;
import java.io.*;
public class ReaderDemo {
public static void main(String[] args) {
String s = "Hello World";
// create a new StringReader
Reader reader = new StringReader(s);
try {
// 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

