Java ByteArrayInputStream reset()方法及示例

Java ByteArrayInputStream reset()方法及示例

reset() 方法是 Java.io.ByteArrayInputStream 的一个内置方法,由mark()方法调用。它将输入流重新定位到标记的位置。

语法:

public void reset()

参数 :该函数不接受任何参数。

返回值 :该函数不返回任何东西。

以下是上述函数的实现:

程序 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());
 
        // 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());
    }
}

输出

5
6
7

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, 4 };
 
        // 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());
 
        exam.mark(1);
 
        // 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());
    }
}

输出

1
2
3

reset() invoked
4
-1

参考资料: https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.html#reset()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程