Java.io.BufferedInputStream.skip() 方法



描述

java.io.BufferedInputStream.skip(long) 方法跳过缓冲输入流中的 n 个字节的数据。跳过的字节数作为 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
java_io_bufferedinputstream.htm
广告
© . All rights reserved.