Java.io.PipedInputStream.available() 方法



描述

java.io.PipedInputStream.available() 方法返回可以從此輸入流讀取而無需阻塞的位元組數。

宣告

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

public int available()

引數

返回值

此方法返回可以從此輸入流讀取而無需阻塞的位元組數,如果此輸入流已透過呼叫其 close() 方法關閉,或者管道未連線或已損壞,則返回 0。

異常

IOException − 如果發生 I/O 錯誤。

示例

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

package com.tutorialspoint;

import java.io.*;

public class PipedInputStreamDemo {
   public static void main(String[] args) {

      // create a new Piped input and Output Stream
      PipedOutputStream out = new PipedOutputStream();
      PipedInputStream in = new PipedInputStream();

      try {
         // connect input and output
         in.connect(out);

         // write something 
         out.write(70);
         out.write(71);

         // print how many bytes are available
         System.out.println("" + in.available());

         // read what we wrote
         for (int i = 0; i < 2; i++) {
            System.out.println("" + (char) in.read());
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

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

2
F
G
java_io_pipedinputstream.htm
廣告