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


Java BufferedInputStream skip()用法及代碼示例

BufferedInputStream類skip()方法

  • skip() 方法可在java.io包。
  • skip() 方法用於從該 BufferedInputStream 跳過給定字節數的內容 (n_bytes)。
  • skip() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
  • skip() 方法可能會在跳過字節時拋出異常。
    IOException: 執行輸入/輸出操作時可能會拋出此異常或un-supports seek()。

用法:

    public long skip(long n_bytes);

參數:

  • long n_bytes– 表示要跳過的字節數。

返回值:

該方法的返回類型是long,它返回要跳過的 n_bytes(即字節數)。

例:

// Java program to demonstrate the example 
// of long skip(long n_bytes) method of 
// BufferedInputStream

import java.io.*;

public class demo {
    public static void main(String[] args) throws Exception {
        // To open text file by using 
        // FileInputStream
        FileInputStream fis = new FileInputStream("e:/includehelp.txt");

        // By using BufferedInputStream() isto change
        // the stream fis into buff_str
        BufferedInputStream buff_str = new BufferedInputStream(fis);

        // By using available() method is to
        // return the no. of bytes to be left 
        // for reading
        Integer n_byte = buff_str.available();
        System.out.println("Left avail bytes = " + n_byte);

        // Read character from the stream
        char ch1 = (char) buff_str.read();
        char ch2 = (char) buff_str.read();
        char ch3 = (char) buff_str.read();
        char ch4 = (char) buff_str.read();

        System.out.println("ch1:" + ch1);
        System.out.println("ch2:" + ch2);
        System.out.println("ch3:" + ch3);
        System.out.println("ch4:" + ch4);

        // It skip 1 bytes of data
        // from the stream  
        buff_str.skip(1);

        char ch = (char) buff_str.read();
        System.out.println("ch:" + ch);

        fis.close();
        buff_str.close();
    }
}

輸出

Left avail bytes = 33
ch1:H
ch2:e
ch3:l
ch4:l
ch:.


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java BufferedInputStream skip() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。