Java ByteArrayInputStream.markSupported()
方法
原文:https://www.studytonight.com/java-file-io/java-bytearrayinputstream-marksupported-method
在本教程中,我们将学习 Java 中Bytearrainputstream类的markSupported()
方法。该方法用于验证流是否支持mark() and
reset()`` 方法。如果流支持布尔值 true,则返回该值,否则返回 false。这是一个非静态方法,在 java.io 包中可用,只能使用类对象访问。如果通过类名访问,它将引发异常。
语法:
这是markSupported()
方法的语法声明,这个方法不接受任何参数。此方法返回一个布尔值,该值指示流的mark()
和reset()
方法的可支持性。
public boolean markSupported()
示例:ByteArrayInputStream
标记支持()方法
这里我们检查当前流是否支持 mark()方法,在我们的例子中,这个方法返回一个真值,这意味着当前流支持 mark()方法。
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class StudyTonight
{
public static void main(String[] args) throws IOException
{
byte[] buf = {1, 2, 3, 4};
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buf);
boolean isMarkSupported = byteArrayInputStream.markSupported();
System.out.println("Is mark supported : "+isMarkSupported);
}
}
是否支持标记:真
示例:ByteArrayInputStream
标记支持()方法
在这里,我们将探索这个markSupported()
方法是如何工作的,首先我们检查这个方法是否支持标记方法,然后我们读取一些数据,在这一点上,我们通过调用 mark()方法来标记位置,然后我们再次读取数据,并在调用 reset 方法时调用 reset()方法,这将把流指针设置为最近标记的位置 snd,在这之后,如果我们调用 read 方法,它将再次从该点读取。
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class StudyTonight
{
public static void main(String[] args) throws IOException
{
try
{
byte[] buf = {65, 66, 67, 68, 69};
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
boolean isMarkSupported = bais.markSupported();
System.out.println("Is mark supported : "+isMarkSupported);
System.out.println("Byte read "+ bais.read());
System.out.println("Byte read "+ bais.read());
System.out.println("Byte read "+ bais.read());
System.out.println("Mark() invocation");
bais.mark(0);
System.out.println("Byte read "+ bais.read());
System.out.println("Byte read "+ bais.read());
System.out.println("Reset() invocation");
bais.reset();
System.out.println("Byte read "+ bais.read());
System.out.println("Byte read "+ bais.read());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
是否支持标记:真 以下为证明: 字节读取 65 字节读取 66 字节读取 67 标记()调用 字节读取 68 字节读取 69 重置()调用 字节读取 68 字节读取 69
结论:
在本教程中,我们学习了 Java 中Bytearrainputstream类的markSupported()
方法。Java 中 bytearrainputstream 类的markSupported()
方法用于验证该流是否支持mark() and
reset()`` 方法。如果流支持布尔值 true,则返回该值,否则返回 false。