Java中的 ByteArrayInputStream markSupported() 方法及示例
markSupported()方法是Java.io.ByteArrayInputStream方法的内置方法,用于测试此输入流是否支持 mark 和 reset 方法。 ByteArrayInputStreamInputStream 的 markSupported 方法总是返回 true
语法:
public boolean markSupported()
参数:该函数不接受任何参数。
返回值:该函数返回一个布尔值。如果此流实例支持 mark 和 reset 方法,则返回 true,否则返回 false。
下面是上述函数的实现:
方案一:
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());
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.");
}
}
}
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 };
// 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.");
}
}
}
输出:
5
6
7
Mark() invocation
markSupported() : true
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 };
// 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: Java/io/ByteArrayInputStream.html#markSupported()