📌  相关文章
📜  Java中的 ByteArrayInputStream reset() 方法及示例

📅  最后修改于: 2022-05-13 01:55:37.093000             🧑  作者: Mango

Java中的 ByteArrayInputStream reset() 方法及示例

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

语法

public void reset()

参数:该函数不接受任何参数。
返回值:该函数不返回任何内容。

下面是上述函数的实现:
方案一:

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


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


输出:
5
6
7

reset() invoked
5
6

方案二:

Java

// 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: Java/io/ByteArrayInputStream.html#reset()