long skip(long n)
描述 (Description)
java.io.PushbackInputStream.skip(long n)方法跳过并从此输入流中丢弃n个字节的数据。 由于各种原因,跳过方法可能最终跳过一些较小数量的字节,可能为零。 如果n为负数,则不跳过任何字节。 PushbackInputStream的skip方法首先跳过回推缓冲区中的字节(如果有)。 如果需要跳过更多字节,它会调用底层输入流的skip方法。 返回跳过的实际字节数。
声明 (Declaration)
以下是java.io.PushbackInputStream.skip()方法的声明。
public long skip(long n)
参数 (Parameters)
n - 要跳过的字节数。
返回值 (Return Value)
此方法返回跳过的实际字节数。
异常 (Exception)
IOException - 如果流不支持seek,或者通过调用close()方法关闭了流,或者发生I/O错误。
例子 (Example)
以下示例显示了java.io.PushbackInputStream.skip()方法的用法。
package com.iowiki;
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