目录

long skip(long n)

描述 (Description)

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

声明 (Declaration)

以下是java.io.PushbackReader.skip()方法的声明。

public long skip(long n)

参数 (Parameters)

n - 要跳过的字符数。

返回值 (Return Value)

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

异常 (Exception)

  • IllegalArgumentException - 如果n为负数。

  • IOException - 如果发生I/O错误。

例子 (Example)

以下示例显示了java.io.PushbackReader.skip()方法的用法。

package com.iowiki;
import java.io.*;
public class PushbackReaderDemo {
   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
↑回到顶部↑
WIKI教程 @2018