Java PipedOutputStream.connect 连接管道输出流

定义

public void connect(PipedInputStream snk)
参数

snk:要连接的管道输入流。

如果snk是未连接的管道输入流,而src是未连接的管道输出流,则可以通过src.connect(snk)或snk.connect(src),这两个调用具有相同的效果。

返回

无任何返回

异常

IOException:如果此对象已经连接到其他管道输入流,则抛出IOException。

例子

public static void main(String[] args)
{
    PipedInputStream in = new PipedInputStream(); //定义管道输入流  
    PipedOutputStream out = new PipedOutputStream(); //定义管道输出流  
    Thread send = new Thread(() - >
    { 
	    //创建多线程通信  
        byte[] buf = new byte[1024];
        int len = 0;
        try
        {
            len = in .read(buf);
            System.out.println(new String(buf, 0, len)); 
			in.close(); //关闭流  
        }
        catch(IOException e)
        {
        }
    });
    Thread recived = new Thread(() - >
    {
        String strInfo = "hello JAVA";
        try
        {
            out.write(strInfo.getBytes()); //写入管道中  
            out.close(); //关闭管道  
        }
        catch(IOException e)
        {
        }
    });
    try
    { 
	in.connect(out); //管道连接  
        send.start(); //启动发送  
        recived.start(); //启动输出  
    }
    catch(IOException e)
    {
    }
}