例子
以下示例显示java.io.BufferedInputStream.skip(long n)方法的用法。
package com.jc2182;
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
让我们编译并运行以上程序,这将产生以下结果-