Java PushbackInputStream skip()方法与示例
PushbackInputStream类skip()方法
skip()方法在java.io包中可用。
skip()方法用于从此PushbackInputStream跳过给定数量的内容字节。当给定参数小于0时,则不跳过任何字节。
skip()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名访问该方法,则会收到错误消息。
skip()方法在跳过数据字节时可能会引发异常。
IOException:在执行或通过其close()方法或流不支持seek()方法关闭流时,如果遇到任何输入/输出错误,则可能引发此异常。
语法:
public long skip(long number);
参数:
longnumber表示要跳过的字节数。
返回值:
该方法的返回类型很长,它返回跳过的确切字节数。
示例
//Java程序演示示例 
//的longskip(longnumber)方法的说明
//PushbackInputStream-
import java.io.*;
public class SkipOfPBIS {
    public static void main(String[] args) throws Exception {
        byte[] b_arr = {
            97,
            98,
            99,
            100,
            101,
            102,
            103,
            104,
            105
        };
        InputStream is_stm = null;
        PushbackInputStream pb_stm = null;
        try {
            //实例化ByteArrayOutputStream和PushbackInputStream-
            is_stm = new ByteArrayInputStream(b_arr);
            pb_stm = new PushbackInputStream(is_stm);
            //循环阅读直到结束
            for (int i = 0; i < 4; ++i) {
                //通过使用read()方法是 
                //将字节转换为char-
                char ch = (char) pb_stm.read();
                System.out.println("ch: " + ch);
                //通过使用skip()方法是
                //跳过给定的数据字节 
                //从流
                long skip = pb_stm.skip(1);
                System.out.println("pb_stm.skip(1): " + skip);
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
        } finally {
            if (is_stm != null)
                is_stm.close();
            if (pb_stm != null)
                pb_stm.close();
        }
    }
}输出结果
ch: a pb_stm.skip(1): 1 ch: c pb_stm.skip(1): 1 ch: e pb_stm.skip(1): 1 ch: g pb_stm.skip(1): 1