/*
集合+IO流 --->Properties类
线程+IO流 --->PipedInputStream PipedOutputStream
*/
import java.io.*;
class Read implements Runnable
{
private PipedInputStream pis;
Read(PipedInputStream pis)
{
this.pis = pis;
}
public void run()
{
try
{
byte[] buf = new byte[1024];
int len = pis.read(buf);
String s = new String(buf,0,len);
System.out.println(s);
pis.close();
}
catch (IOException e)
{
throw new RuntimeException("管道读取流失败");
}
}
}
class Write implements Runnable
{
private PipedOutputStream pos;
Write(PipedOutputStream pos)
{
this.pos = pos;
}
public void run()
{
try
{
pos.write("piped lai la".getBytes());
pos.close();
}
catch (IOException e)
{
throw new RuntimeException("管道写入流失败");
}
}
}
class PipedStreamDemo
{
public static void main(String[] args) throws IOException
{
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream();
pis.connect(pos);
Write w = new Write(pos);
Read r = new Read(pis);
new Thread(w).start();
new Thread(r).start();
}
}
day21/PipedStreamDemo.java
最新推荐文章于 2022-01-30 10:37:52 发布