Java.io.PushbackInputStream.available() 方法



描述

java.io.PushbackInputStream.available() 方法返回一個估計值,表示在該輸入流的下一個方法呼叫(可能來自同一執行緒或其他執行緒)阻塞之前,可以從此輸入流讀取(或跳過)的位元組數。讀取或跳過這麼多位元組的單個操作不會阻塞,但可能會讀取或跳過更少的位元組。

宣告

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

public int available()

引數

返回值

此方法返回可以在不阻塞的情況下從輸入流讀取(或跳過)的位元組數。

異常

IOException − 如果透過呼叫其 close() 方法關閉了此輸入流,或者發生 I/O 錯誤。

示例

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

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'};

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

         // check how many bytes are available
         System.out.println("" + pis.available());

         // read from the buffer one character at a time
         for (int i = 0; i < byteArray.length; 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();
      }
   }
}

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

5
Hello
java_io_pushbackinputstream.htm
廣告

© . All rights reserved.