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.