當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java 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.skip() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。