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


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


Java中BufferedReader类的reset()方法用于将位置固定或标记在最后标记的位置,以便可以再次读取相同的字节。

用法:

public void reset() 
            throws IOException

覆盖:覆盖Reader类的reset()方法。

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

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



异常:如果从不在流上调用mark()方法,或者mark的值无效,则该方法将引发IOException。

以下示例程序旨在说明IO包中BufferedReader类中的reset()方法:

程序1:假设存在文件“c:/demo.txt”。

// Java program to illustrate 
// BufferedReader reset() method 
  
import java.io.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Read the stream 'demo.txt' 
        // for containing text "GEEKS" 
        FileReader fileReader 
            = new FileReader( 
                "c:/demo.txt"); 
  
        // Convert fileReader to 
        // bufferedReader 
        BufferedReader buffReader 
            = new BufferedReader( 
                fileReader); 
  
        // Read and print characters 
        // one by one 
        System.out.println( 
            "Char:"
            + (char)buffReader.read()); 
        System.out.println( 
            "Char:"
            + (char)buffReader.read()); 
        System.out.println( 
            "Char:"
            + (char)buffReader.read()); 
  
        // Mark is set on the stream 
        buffReader.mark(0); 
  
        System.out.println( 
            "Char:"
            + (char)buffReader.read()); 
  
        // Reset() is invoked 
        buffReader.reset(); 
  
        // Read and print characters 
        System.out.println( 
            "Char:"
            + (char)buffReader.read()); 
        System.out.println( 
            "Char:"
            + (char)buffReader.read()); 
    } 
}
输出:
Char:G
Char:E
Char:E
Char:K
Char:K
Char:S

程序2:假设存在文件“c:/demo.txt”。

// Java program to illustrate 
// BufferedReader reset() method 
import java.io.*; 
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Read the stream 'demo.txt' 
        // containing text "GEEKSFORGEEKS" 
        FileReader fileReader 
            = new FileReader( 
                "c:/demo.txt"); 
  
        // Convert fileReader to 
        // bufferedReader 
        BufferedReader buffReader 
            = new BufferedReader( 
                fileReader); 
  
        // Read and print characters 
        // one by one 
        System.out.println( 
            "Char:"
            + (char)buffReader.read()); 
        System.out.println( 
            "Char:"
            + (char)buffReader.read()); 
        System.out.println( 
            "Char:"
            + (char)buffReader.read()); 
        System.out.println( 
            "Char:"
            + (char)buffReader.read()); 
  
        // Mark is set on the stream 
        buffReader.mark(0); 
  
        System.out.println( 
            "Char:"
            + (char)buffReader.read()); 
  
        // Reset() is invoked 
        buffReader.reset(); 
  
        // read and print characters 
        System.out.println( 
            "Char:"
            + (char)buffReader.read()); 
        System.out.println( 
            "Char:"
            + (char)buffReader.read()); 
        System.out.println( 
            "Char:"
            + (char)buffReader.read()); 
        System.out.println( 
            "Char:"
            + (char)buffReader.read()); 
    } 
}
输出:
Char:G
Char:E
Char:E
Char:K
Char:S
Char:S
Char:F
Char:O
Char:R

参考文献:
https://docs.oracle.com/javase/10/docs/api/java/io/BufferedReader.html#reset()




相关用法


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