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


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


Java中BufferedReader類的mark()方法用於標記緩衝區讀取器流中的當前位置。在調用mark()方法之後,也將隨後調用同一BufferedReader類的reset()方法。 reset()方法將位置固定在最後標記的位置,以便可以再次讀取相同的字節。

用法:

public void mark(int readAheadLimit) 
                   throws IOException

覆蓋:覆蓋Reader類的mark()方法。

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

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



異常:此方法可以引發兩種類型的異常。

  • IllegalArgumentException-如果傳遞的參數readAheadLimit小於零,則拋出此異常。
  • IOException-如果發生I /O錯誤,則拋出此異常。

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

// Java program to illustrate 
// BufferedReader mark() 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 mark() 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#mark(int)




相關用法


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