Java ByteArrayInputStream skip()方法及示例
skip() 方法是 Java.io.ByteArrayInputStream 的一个内置方法,它跳过输入流中的arg字节。如果流已经到达终点,则跳过较少的字节。
语法:
public long skip(long args)
参数 :该函数接受一个强制性参数 args ,该参数指定要跳过的字节数。
返回值 :该函数返回跳过的字节数。
错误和异常 :当发生I/O错误时,该函数会抛出一个IOException。
下面是上述函数的实现。
程序 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());
// Skips 1 element
exam.skip(1);
System.out.println(exam.read());
System.out.println(exam.read());
}
}
输出。
5
7
8
程序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, 5, 6, 7, 8 };
// Create new byte array input stream
ByteArrayInputStream exam
= new ByteArrayInputStream(buf);
// print bytes
System.out.println(exam.read());
// Skips 3 elements
exam.skip(3);
System.out.println(exam.read());
System.out.println(exam.read());
}
}
输出。
1
5
6
参考资料: https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.html#skip(长)
极客教程