管道流 pipedinputstream
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了管道流 pipedinputstream相关的知识,希望对你有一定的参考价值。
public class StreamDemo3 {
public static void main(String[] args) {
try {
//管道流
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream(in);
Send send = new Send(out);
Receiver rec = new Receiver(in);
send.send();
rec.rec();
//关闭省略了
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Send{
OutputStream out;
public Send(OutputStream out) {
super();
this.out = out;
}
public void send(){
try {
byte value =(byte)(Math.random()*100);
System.out.println("send the value is:"+value);
out.write(value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Receiver{
InputStream in;
public Receiver(InputStream in) {
super();
this.in = in;
}
public void rec(){
try {
byte value =(byte)in.read();
System.out.println("rec the value is :"+value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
以上是关于管道流 pipedinputstream的主要内容,如果未能解决你的问题,请参考以下文章
字节输入流/输出流-----PipedInputStream/PipedOutputStream
PipedOutputStream&PipedInputStream---管道流