📜  Java中的 StringReader reset() 方法及示例

📅  最后修改于: 2022-05-13 01:55:09.974000             🧑  作者: Mango

Java中的 StringReader reset() 方法及示例

Java中StringReader类的reset()方法用于重置流。重置后,如果流已被标记,则此方法尝试将其重新定位在标记处,否则它将尝试将其定位到起始位置。

句法:

public void reset()

参数:此方法不接受任何参数。

返回值:此方法不返回任何值。

异常:此方法抛出 IOException:

  • 如果输入输出时出现错误
  • 如果流没有被标记
  • 如果商标已失效
  • 如果不支持 reset() 方法。

下面的方法说明了 reset() 方法的工作:

方案一:

// Java program to demonstrate
// StringReader reset() method
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
            String str = "GeeksForGeeks";
  
            // Create a StringReader instance
            StringReader reader
                = new StringReader(str);
  
            // Get the character
            // to be read from the stream
            int ch;
  
            // Read the first 10 characters
            // to this reader using read() method
            // This will put the str in the stream
            // till it is read by the reader
            for (int i = 0; i < 10; i++) {
                ch = reader.read();
                System.out.print((char)ch);
            }
  
            System.out.println();
  
            // mark the stream for
            // 5 characters using reset()
            reader.mark(5);
  
            // reset the stream position
            reader.reset();
  
            // Read the 5 characters from marked position
            // to this reader using read() method
            for (int i = 0; i < 5; i++) {
                ch = reader.read();
                System.out.print((char)ch);
            }
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
GeeksForGe
eks??

方案二:

// Java program to demonstrate
// StringReader reset() method
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
            String str = "GeeksForGeeks";
  
            // Create a StringReader instance
            StringReader reader
                = new StringReader(str);
  
            // Get the character
            // to be read from the stream
            int ch;
  
            // Read the first 10 characters
            // to this reader using read() method
            // This will put the str in the stream
            // till it is read by the reader
            for (int i = 0; i < 10; i++) {
                ch = reader.read();
                System.out.print((char)ch);
            }
  
            System.out.println();
  
            // reset the stream position
            reader.reset();
  
            // Read the 5 characters from marked position
            // to this reader using read() method
            for (int i = 0; i < 5; i++) {
                ch = reader.read();
                System.out.print((char)ch);
            }
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
GeeksForGe
Geeks

参考: https://docs.oracle.com/javase/9/docs/api/ Java/io/StringReader.html#reset–