Java PushbackReader skip()方法

java.io.PushbackReader.skip(long n) 方法跳过字符。此方法将阻塞,直到有字符可用,发生I/ O错误,或者该流的末尾。

1 语法

public long skip(long n)

2 参数

n:要跳过的字符数。

3 返回值

此方法返回实际跳过的字符数。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.PushbackReader.skip(long n)方法的例子
 */
import java.io.*;

public class Demo {
    public static void main(String[] args) {
        String s = "Hello World";

        // create a new StringReader
        StringReader sr = new StringReader(s);

        // create a new PushBack reader based on our string reader
        PushbackReader pr = new PushbackReader(sr, 20);

        try {
            // read the first five chars
            for (int i = 0; i < 5; i++) {
                char c = (char) pr.read();
                System.out.print("" + c);

                // skip a character every time
                pr.skip(1);
            }

            // change line
            System.out.println();

            // close the stream
            pr.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

输出结果为:

HloWr

热门文章

优秀文章