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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。