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


Java BufferedInputStream mark()用法及代碼示例


Java中BufferedInputStream類的mark()方法用於標記輸入流中的當前位置。在mark()方法之後,調用相同類BufferedInputStream的Reset()方法。 Reset()將位置固定在最後標記的位置,以便可以再次讀取相同的字節。

常規合同:輸入流以某種方式保存了在調用mark()方法之後讀取的所有字節,並在調用reset()方法時再次返回相同的字節。僅當markSupported()返回true時,才執行此操作。但是,如果在調用reset()方法之前讀取的字節超過了readlimit字節,則輸入流不需要保存任何數據。

用法:



public void mark(int readlimit)

覆蓋:重寫FilterInputStream類的mark()方法。

參數:此方法接受一個I​​nteger類型的參數readlimit,它表示標記位置變為無效之前可以讀取的最大字節數限製。

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

異常:此方法不會引發任何異常。

以下示例程序旨在說明IO包中BufferedInputStream類中的mark()方法:
程序1:假設存在文件“c:/demo.txt”。

// Java program to illustrate 
// BufferedInputStream mark() 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()); 
  
        // Reset() is invoked 
        buffInputStr.reset(); 
  
        // Read and print characters 
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
        System.out.println( 
            "Char:"
            + (char)buffInputStr.read()); 
    } 
}
輸出:
Char:G
Char:E
Char:E
Char:K
Char:K
Char:S

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

// Java program to illustrate 
// BufferedInputStream.mark() 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()); 
  
        // 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()); 
    } 
}
輸出:
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/BufferedInputStream.html#mark(int)




相關用法


注:本文由純淨天空篩選整理自pp_pankaj大神的英文原創作品 BufferedInputStream mark() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。