當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java PipedInputStream receive()用法及代碼示例


PipedInputStream類receive()方法

  • receive() 方法可在java.io包。
  • receive() 方法用於接收一個字節的內容,當沒有更多輸入時它將阻塞。
  • receive() 方法是一個非靜態方法,它隻能通過類對象訪問,如果我們嘗試使用類名訪問方法,那麽我們將得到一個錯誤。
  • receive() 方法可能在接收到一個字節的數據時拋出異常。
    IOException:當遇到任何輸入/輸出錯誤或管道未正確連接或流關閉時,可能會拋出此異常。

用法:

    protected void receive(int byt);

參數:

  • int byt– 表示接收到的字節。

返回值:

該方法的返回類型是void,它什麽都不返回。

例:

// Java program to demonstrate the example 
// of receive(int byt) method of PipedInputStream

import java.io.*;

public class ReceiveOfPIS extends PipedInputStream {
    public static void main(String[] args) throws Exception {
        int val = 65;
        
        try {
            // Instantiates ReceiveOfPIS and 
            // PipedOutputStream
            ReceiveOfPIS pipe_in = new ReceiveOfPIS();
            PipedOutputStream pipe_out = new PipedOutputStream();

            // By using connect() method is to
            // connect this pipe_in to the given pipe_out
            pipe_in.connect(pipe_out);

            for (int i = 0; i < 3; ++i) {
                // By using write() method is to
                // write the val to the stream pipe_out
                pipe_out.write(val);
                val++;
            }

            for (int i = 1; i < 4; ++i) {
                // By using receive() method is to
                // receive a byte
                pipe_in.receive(val);
                System.out.println(i + " " + "Byte Received ");
            }

            // By using close() method is to close
            // the stream
            pipe_in.close();
            pipe_out.close();
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }
}

輸出

1 Byte Received 
2 Byte Received 
3 Byte Received 


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java PipedInputStream receive() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。