Java InputStream.skip()跳过并丢弃输入流的n个字节

定义

public long skip(long n)
参数

n:要跳过的字节数。

返回

跳过的实际字节数。

异常

IOException:I/O 错误或者流不支持seek。

实例

public static void main(String[] args) throws Exception
{
    InputStream is = null;
    int i;
    try
    {
        is = new FileInputStream("C://51gjie.txt");
        while((i = is.read()) != -1)
        {
            char c = (char) i;
            System.out.println("字符: " + c);
            is.skip(1); //跳过1个字符
        }
    }
    catch(Exception e)
    {}
    finally
    {
        if(is != null) is.close();
    }
}

InputStream.skip()此方法是从输入流的当前位置往后跳过n个字符,这样这n个字符就丢失了。当然如果你要找回,可以直接mark,并且reset当前的流位置,重新获取流就可以了。