Java ByteArrayInputStream mark()方法及示例
mark() 方法是 Java.io.ByteArrayInputStream 的一个内置方法,用于标记输入流的当前位置。它设置了readlimit,即在标记位置变得无效之前可以读取的最大字节数。
语法:
public void mark(int arg)
参数 :该函数接受一个强制参数 arg ,它指定了在标记位置变得无效之前可以读取的最大限制字节数。
返回值 :该函数不返回任何东西。
下面是上述函数的实现。
程序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");
// mark() invocation;
exam.mark(0);
System.out.println(exam.read());
System.out.println(exam.read());
}
}
输出。
5
6
7
Mark() invocation
8
9
程序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("Mark() invocation");
// mark() invocation;
exam.mark(3);
System.out.println(exam.read());
System.out.println(exam.read());
}
}
输出。
1
Mark() invocation
2
3
参考资料: https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.html#mark(int)
极客教程