Java BufferedInputStream skip()方法

java.io.BufferedInputStream.skip(long) 跳过并从输入流中丢弃x字节的数据。

1 语法

public long skip(long n)

2 参数

n:要跳过的字节数

3 返回值

返回要跳过的实际字节数。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.BufferedInputStream.skip(long)方法的例子
 */
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Demo {
    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("d:/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();
        }
    }
}

假设test.txt内容如下:

ABCDE

输出结果为:

 B D ￿

热门文章

优秀文章