Java ByteArrayInputStream markSupported()方法及示例

Java ByteArrayInputStream markSupported()方法及示例

markSupported() 方法是 Java.io.ByteArrayInputStream 方法的一个内置方法,测试这个输入流是否支持标记和重置方法。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 stream 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 stream 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()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程