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


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

BufferedReader類skip()方法

  • skip() 方法可在java.io包。
  • skip() 方法用於從此 BufferedReader 跳過給定字節數的字符 (n_bytes_of_char)。
  • skip() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
  • skip() 方法可能會在跳過字節時拋出異常。
    • IOException: 執行輸入/輸出操作時可能會拋出此異常。
    • IllegalArgumentException:當給定的參數無效時可能會拋出此異常。

用法:

    public long skip(long n_bytes_of_char);

參數:

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

返回值:

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

例:

// Java program to demonstrate the example 
// of long skip(long n_bytes_of_char) method of 
// BufferedReader

import java.io.*;

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

        // Instantiates InputStreamReader 
        InputStreamReader inp_r = new InputStreamReader(fis);

        // Instantiates BufferedReader 
        BufferedReader buff_r = new BufferedReader(inp_r);

        // Read character from the stream
        char ch1 = (char) buff_r.read();
        char ch2 = (char) buff_r.read();
        char ch3 = (char) buff_r.read();
        char ch4 = (char) buff_r.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_r.skip(1);

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

        fis.close();
        inp_r.close();
        buff_r.close();
    }
}

輸出

ch1:H
ch2:e
ch3:l
ch4:l
ch:.


相關用法


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