当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。