当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Reader reset()用法及代码示例


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

用法:

public void reset()

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


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

异常:此方法引发IOException:

  • 如果输入输出时发生一些错误
  • 如果尚未标记流
  • 商标是否无效
  • 如果不支持reset()方法。

下面的方法说明了reset()方法的用法方式:

示例1:

// Java program to demonstrate 
// Reader reset() method 
  
import java.io.*; 
import java.util.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        try { 
  
            String str = "GeeksForGeeks"; 
  
            // Create a Reader instance 
            Reader 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??

示例2:

// Java program to demonstrate 
// Reader reset() method 
  
import java.io.*; 
import java.util.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        try { 
            String str = "GeeksForGeeks"; 
  
            // Create a Reader instance 
            Reader 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/Reader.html#reset–



相关用法


注:本文由纯净天空筛选整理自Code_r大神的英文原创作品 Reader reset() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。