Java PipedInputStream receive()方法与示例

PipedInputStream类的receive()方法 (PipedInputStream Class receive() method)

  • receive() method is available in java.io package.

    receive()方法在java.io包中可用。

  • receive() method is used to receive a byte of content and it will block when no more input remains.

    receive()方法用于接收内容的一个字节,当不再有剩余输入时它将阻塞。

  • receive() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    receive()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • receive() method may throw an exception at the time of receiving a byte of data.

    receive()方法在接收到一个字节数据时可能会引发异常。

    IOException: This exception may throw when getting any input/output error or pipe not connected properly, or stream closed.

    IOException :当出现任何输入/输出错误或管道未正确连接或流关闭时,可能引发此异常。

Syntax:

句法:

    protected void receive(int byt);

Parameter(s):

参数:

  • int byt – represents the byte getting received.

    int byt –表示要接收的字节。

Return value:

返回值:

The return type of the method is void, it returns nothing.

该方法的返回类型为void ,不返回任何内容。

Example:

例:

// 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());
        }
    }
}

Output

输出量

1 Byte Received 
2 Byte Received 
3 Byte Received 


翻译自: https://www.includehelp.com/java/pipedinputstream-receive-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值