Java.io.PipedInputStream.read() 方法



描述

java.io.PipedInputStream.receive(int b) 方法接收一個位元組的資料。如果沒有任何輸入可用,此方法將阻塞。

宣告

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

protected void receive(int b)

引數

b − 接收的位元組

返回值

此方法不返回值。

異常

IOException − 如果管道損壞、未連線、已關閉或發生 I/O 錯誤。

示例

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

package com.tutorialspoint;

import java.io.*;

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

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

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

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

         // receive a byte 
         System.out.println("Receiving Byte...");
         in.receive(71);
         System.out.println("Byte Received.");
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

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

Receiving Byte...
Byte Received.
java_io_pipedinputstream.htm
廣告