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


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

StringReader類skip()方法

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

用法:

    public long skip(long number);

參數:

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

返回值:

該方法的返回類型是long,它返回跳過的確切字符數。

例:

// Java program to demonstrate the example 
// of long skip(long number) method of StringReader

import java.io.*;

public class SkipOfSR {
    public static void main(String[] args) throws Exception {
        StringReader str_r = null;

        try {
            // Instantiates StringReader
            str_r = new StringReader("Java World");

            // Loop to read until available
            // bytes left
            for (int val = 0; val <= 4; ++val) {
                // Read corresponding char value
                char ch = (char) str_r.read();

                // Display value of ch
                System.out.println("ch:" + ch + " ");

                // By using skip() method is
                // to skip 1 bytes of data
                // from str_r	    

                long skip = str_r.skip(1);
                System.out.println("str_r.skip(1):" + skip);
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());

        } finally {
            // with the help of this block is to
            // free all necessary resources linked
            // with the stream
            if (str_r != null) {
                str_r.close();
            }
        }
    }
}

輸出

ch:J 
str_r.skip(1):1
ch:v 
str_r.skip(1):1
ch:  
str_r.skip(1):1
ch:o 
str_r.skip(1):1
ch:l 
str_r.skip(1):1


相關用法


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