Java.io.LineNumberInputStream.skip() 方法



描述

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

宣告

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

public long skip(long n)

引數

n - 要跳過的位元組數。

返回值

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

異常

IOException - 如果發生 I/O 錯誤。

示例

以下示例顯示了 java.io.LineNumberInputStream.skip(long n) 方法的使用。

package com.tutorialspoint;

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

public class LineNumberInputStreamDemo {
   public static void main(String[] args) throws IOException {
      LineNumberInputStream lnis = null;
      FileInputStream fis = null;
      int i;
      char c;
      
      try {
         // create new input stream
         fis = new FileInputStream("C:/test.txt");
         lnis = new LineNumberInputStream(fis);
         
         // read till the end of the file
         while((i = lnis.read())!=-1) {
         
            // converts int to char
            c = (char)i;
            
            // prints
            System.out.println(c);
            
            // skips one byte
            lnis.skip(1);
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      } finally {
         // closes the stream and releases any system resources
         if(fis!=null)
            fis.close();
         if(lnis!=null)
            lnis.close();      
      }
   }
}

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

ABCDE

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

A
C
E
java_io_linenumberinputstream.htm
廣告

© . All rights reserved.