void 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