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


Java ByteArrayInputStream markSupported()用法及代碼示例


markSupported()方法是Java.io.ByteArrayInputStream方法的內置方法,用於測試此輸入流是否支持mark和reset方法。 ByteArrayInputStreamInputStream的markSupported方法始終返回true

用法

public boolean markSupported()

參數:該函數不接受任何參數。


返回值:該函數返回一個布爾值。如果此流實例支持mark和reset方法,則返回true,否則返回false。

下麵是上述函數的實現:

示例1:

// Java program to implement 
// the above function 
import java.io.*; 
  
public class Main { 
    public static void main(String[] args) throws Exception 
    { 
  
        byte[] buf = { 5, 6, 7, 8, 9 }; 
  
        // Create new byte array input stream 
        ByteArrayInputStream exam 
            = new ByteArrayInputStream(buf); 
  
        // print bytes 
        System.out.println(exam.read()); 
        System.out.println(exam.read()); 
        System.out.println(exam.read()); 
  
        System.out.println("Mark() invocation"); 
  
        // Use of markSupported 
        boolean check = exam.markSupported(); 
        System.out.println("markSupported() : "
                           + check); 
  
        if (exam.markSupported()) { 
  
            // Use of reset() method : 
            // repositioning the stram to marked positions. 
            exam.reset(); 
  
            System.out.println("\nreset() invoked"); 
            System.out.println(exam.read()); 
            System.out.println(exam.read()); 
        } 
        else { 
            System.out.println("reset() method not supported."); 
        } 
    } 
}
輸出:
5
6
7
Mark() invocation
markSupported() : true

reset() invoked
5
6

示例2:

// Java program to implement 
// the above function 
import java.io.*; 
  
public class Main { 
    public static void main(String[] args) throws Exception 
    { 
  
        byte[] buf = { 1, 2, 3 }; 
  
        // Create new byte array input stream 
        ByteArrayInputStream exam 
            = new ByteArrayInputStream(buf); 
  
        // print bytes 
        System.out.println(exam.read()); 
        System.out.println(exam.read()); 
        System.out.println(exam.read()); 
  
        // Use of markSupported 
        boolean check = exam.markSupported(); 
  
        System.out.println("markSupported() : "
                           + check); 
  
        if (exam.markSupported()) { 
  
            // Use of reset() method : 
            // repositioning the stram to marked positions 
            exam.reset(); 
  
            System.out.println("\nreset() invoked"); 
            System.out.println(exam.read()); 
            System.out.println(exam.read()); 
        } 
        else { 
            System.out.println("reset() method not supported."); 
        } 
    } 
}
輸出:
1
2
3
markSupported() : true

reset() invoked
1
2

參考: https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.html#markSupported()



相關用法


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