當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。