Java.io.FileInputStream.skip() 方法



描述

java.io.FileInputStream.skip(long n) 方法跳過並丟棄輸入流中的 n 個位元組資料。

宣告

以下是 java.io.FileInputStream.skip(long n) 方法的宣告:

public long skip(long n)

引數

n - 要跳過的位元組數。

返回值

該方法返回實際跳過的位元組數。

異常

IOException - 如果發生 I/O 錯誤,如果 n 為負數或流不支援查詢。

示例

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

package com.tutorialspoint;

import java.io.IOException;
import java.io.FileInputStream;

public class FileInputStreamDemo {
   public static void main(String[] args) throws IOException {
      FileInputStream fis = null;
      int i = 0;
      char c;
            
      try {
         // create new file input stream
         fis = new FileInputStream("C://test.txt");
         
         // skip bytes from file input stream
         fis.skip(4);
         
         // read bytes from this stream
         i = fis.read();
         
         // converts integer to character
         c = (char)i;
         
         // prints
         System.out.print("Character read: "+c);
   
      } catch(Exception ex) {
         // if any error occurs
         ex.printStackTrace();
      } finally {
         // releases all system resources from the streams
         if(fis!=null)
            fis.close();
      }
   }
}

假設我們有一個文字檔案 c:/test.txt,其內容如下。此檔案將用作我們示例程式的輸入:

ABCDEF

讓我們編譯並執行上述程式,這將產生以下結果:

Character read: E
java_io_fileinputstream.htm
廣告