Java.io.Reader.skip() 方法

描述

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


声明

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

public long skip(long n)

参数

n − 要跳过的字符数。


返回值

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


异常

  • IllegalArgumentException − 如果 n 为负数。

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


示例

下面的例子展示了 java.io.Reader.skip() 方法的使用。

package com.tutorialspoint;

import java.io.*;

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

      // create a new StringReader
      Reader reader = new StringReader(s);

      try {
         // read the first five chars
         for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();

            // skip a char every time
            reader.skip(1);

            System.out.print("" + c);
         }

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

         // close the stream
         reader.close();

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

让我们编译并运行上面的程序,这将产生下面的结果 −

HloWr