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


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


Java中BufferedInputStream类的reset()方法用于将流的位置重置为上次调用mark方法时的位置。与同一类的mark()方法组合使用。

总合同:
有两种情况:

  1. 如果支持mark()和reset(),即markSupported返回true,
    • 如果自创建输入流以来未调用mark()或自上次调用mark()方法以来从输入流读取的字节,则未引发mark(),则可能引发IOException。
    • 如果未抛出IOException,则将输入流重置为以下状态:将最近一次调用mark()之后读取的所有字节再次提供给read()的后续调用者。随后是在reset()调用时将具有的字节。
  2. 如果不支持mark()和reset(),即markSupported返回false,
    • 调用reset()方法可能会引发IOException。
    • 如果未抛出IOException,则将输入流重置为固定状态,该状态取决于输入流的特定类型。提供给read()的后续调用者的字节取决于输入流的特定类型。

用法:

public void reset()

覆盖:该方法覆盖FilterInputStream中的reset类。

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



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

异常:如果尚未对此流进行标记或标记已失效,或者通过调用close()方法关闭了输入流,则此方法将引发IOException或发生I /O错误。

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

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

// Java program to illustrate 
// BufferedInputStream reset() method 
import java.io.*; 
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Create input stream 'demo.txt' 
        // for reading containing text "GEEKS" 
        FileInputStream inputStream 
            = new FileInputStream( 
                "c:/demo.txt"); 
  
        // Convert inputStream to 
        // bufferedInputStream 
        BufferedInputStream buffInputStr 
            = new BufferedInputStream( 
                inputStream); 
  
        // Read and print characters one by one 
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
  
        // Mark is set on the input stream 
        buffInputStr.mark(0); 
  
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
  
        System.out.println( 
            "reset() called"); 
  
        // Reset() is invoked 
        buffInputStr.reset(); 
  
        // Read and print characters 
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
    } 
}
输入:
输出:

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

// Java program to illustrate 
// BufferedInputStream reset() method 
import java.io.*; 
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Create input stream 'demo.txt' 
        // for reading containing text 
        // "GEEKSFORGEEKS" 
        FileInputStream inputStream 
            = new FileInputStream( 
                "c:/demo.txt"); 
  
        // Convert inputStream to 
        // bufferedInputStream 
        BufferedInputStream buffInputStr 
            = new BufferedInputStream( 
                inputStream); 
  
        // Read and print characters one by one 
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
  
        // Mark is set on the input stream 
        buffInputStr.mark(0); 
  
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
  
        System.out.println( 
            "reset() called"); 
  
        // Reset() is invoked 
        buffInputStr.reset(); 
  
        // Read and print characters 
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
    } 
}
输入:
输出:

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




相关用法


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