用的频率不高
特点:读取管道和写入管道对接,需要是用多线程技术,单线程容易死锁
使用connect方法连接两个流,实现边读编写,和node.js的管道流差不多
//##主函数位置
public static void main(String[] args) throws IOException
{
//创建两个管道对象
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();
//将两个管道连接上
pis.connect(pos);
new Thread(new Input(pis)).start();;
new Thread(new Output(pos)).start();
}
//定义输入任务
class Input implements Runnable{
private PipedInputStream pis;
public Input(PipedInputStream pis) {
super();
this.pis = pis;
}
@Override
public void run()
{
byte[] buf = new byte[1024];
int len = 0;
try {
len = pis.read(buf);
String messege = new String(buf,0,len);
System.out.println(messege);
pis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//定义输出任务
class Output implements Runnable{
private PipedOutputStream pos;
public Output(PipedOutputStream pos) {
super();
this.pos = pos;
}
@Override
public void run()
{
try {
pos.write("hei,我很想你".getBytes());
pos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
来源:https://www.cnblogs.com/-nbloser/p/9626481.html