目录

long skip(long n)

描述 (Description)

java.io.BufferedInputStream.skip(long)方法从缓冲的输入流中跳过n个字节的数据。 跳过id的字节数返回为long 。 对于负n,不跳过任何字节。

BufferedInputStream的skip方法创建一个字节数组,该字节数组被读入,直到读取n个字节或到达流的末尾。

声明 (Declaration)

以下是java.io.BufferedInputStream.skip(long n)方法的声明。

public long skip(long n)

参数 (Parameters)

n - 要跳过的字节数。

返回值 (Return Value)

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

异常 (Exception)

IOException - 如果流不支持seek,或者发生其他I/O错误。

例子 (Example)

以下示例显示了java.io.BufferedInputStream.skip(long n)方法的用法。

package com.iowiki;
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
↑回到顶部↑
WIKI教程 @2018