Java.io.PushbackInputStream.skip() 方法



描述

java.io.PushbackInputStream.skip(long n) 方法跳過並丟棄此輸入流中的 n 個位元組的資料。由於各種原因,skip 方法最終可能跳過較少數量的位元組,甚至可能為零。如果 n 為負數,則不跳過任何位元組。PushbackInputStream 的 skip 方法首先跳過推送緩衝區中的位元組(如果有)。如果需要跳過更多位元組,則呼叫底層輸入流的 skip 方法。返回實際跳過的位元組數。

宣告

以下是java.io.PushbackInputStream.skip() 方法的宣告。

public long skip(long n)

引數

n − 要跳過的位元組數。

返回值

此方法返回實際跳過的位元組數。

異常

IOException − 如果流不支援查詢,或者透過呼叫其 close() 方法關閉了流,或者發生了 I/O 錯誤。

示例

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

package com.tutorialspoint;

import java.io.*;

public class PushbackInputStreamDemo {
   public static void main(String[] args) {
      
      // declare a buffer and initialize its size:
      byte[] arrByte = new byte[1024];

      // create an array for our message
      byte[] byteArray = new byte[]{'H', 'e', 'l', 'l', 'o',};


      // create object of PushbackInputStream class for specified stream
      InputStream is = new ByteArrayInputStream(byteArray);
      PushbackInputStream pis = new PushbackInputStream(is);
      
      try {
         // skip a byte
         pis.skip(1);

         // read from the buffer one character at a time
         for (int i = 0; i < byteArray.length - 1; i++) {

            // read a char into our array
            arrByte[i] = (byte) pis.read();

            // display the read byte
            System.out.print((char) arrByte[i]);
         }
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

ello
java_io_pushbackinputstream.htm
廣告
© . All rights reserved.