Java.io.PushbackInputStream.skip() 方法
描述
java.io.PushbackInputStream.skip(long n) 方法跳过并丢弃此输入流中的 n 字节数据。 由于各种原因,skip 方法最终可能会跳过一些较小的字节数,可能为零。 如果 n 为负数,则不跳过任何字节。 PushbackInputStream 的 skip 方法首先跳过 pushback 缓冲区中的字节(如果有)。 然后,如果需要跳过更多字节,它会调用底层输入流的 skip 方法。 返回实际跳过的字节数。
声明
以下是 java.io.PushbackInputStream.skip() 方法的声明。
public long skip(long n)
参数
n − 要跳过的字节数。
返回值
此方法返回实际跳过的字节数。
异常
IOException − 如果流不支持查找,或者流已经通过调用其 close() 方法关闭,或者发生 I/O 错误。
示例
下面的例子展示了 java.io.PushbackInputStream.skip() 方法的使用。
package com.tutorialspoint;
import java.io.*;
public class PushbackInputStreamDemo {
public static void main(String[] args) {
// declare a buffer and initialize its size:
byte[] arrByte = new byte[1024];
// create an array for our message
byte[] byteArray = new byte[]{'H', 'e', 'l', 'l', 'o',};
// create object of PushbackInputStream class for specified stream
InputStream is = new ByteArrayInputStream(byteArray);
PushbackInputStream pis = new PushbackInputStream(is);
try {
// skip a byte
pis.skip(1);
// read from the buffer one character at a time
for (int i = 0; i < byteArray.length - 1; i++) {
// read a char into our array
arrByte[i] = (byte) pis.read();
// display the read byte
System.out.print((char) arrByte[i]);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
ello

