Java.io.BufferedInputStream.skip() 方法
描述
java.io.BufferedInputStream.skip(long) 方法从缓冲的输入流中跳过 n 个字节的数据。 跳过的字节数 id 返回为 long。 对于负 n,不跳过任何字节。
BufferedInputStream 的 skip 方法创建一个字节数组,该数组被读取到 n 个字节被读取或到达流的末尾。
声明
以下是 java.io.BufferedInputStream.skip(long n) 方法的声明。
public long skip(long n)
参数
n − 要跳过的字节数。
返回值
返回要跳过的实际字节数。
异常
IOException − 如果流不支持查找,或者发生其他 I/O 错误。
示例
下面的例子展示了 java.io.BufferedInputStream.skip(long n) 方法的使用。
package com.tutorialspoint;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class BufferedInputStreamDemo {
public static void main(String[] args) throws Exception {
InputStream is =null;
BufferedInputStream bis = null;
try {
// open input stream test.txt for reading purpose.
is = new FileInputStream("C:/test.txt");
// input stream is converted to buffered input stream
bis = new BufferedInputStream(is);
// read until a single byte is available
while(bis.available()>0) {
// skip single byte from the stream
bis.skip(1);
// read next available byte and convert to char
char c = (char)bis.read();
// print character
System.out.print(" " + c);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// releases resources from the streams
if(is!=null)
is.close();
if(bis!=null)
bis.close();
}
}
}
假设我们有一个文本文件c:/test.txt,其内容如下。 该文件将用作我们示例程序的输入 −
ABCDEFGHIJKLMNOPQRSTUVWXYZ
让我们编译并运行上面的程序,这将产生下面的结果 −
B D F H J L N P R T V X Z

