Java管道Pipe
2015-09-07 21:14
316 查看
最近在复习Java基础,碰到了管道,之前都没怎么用过,所以认真看了一下。Java中的管道流是做什么用的呢?它与一般输出输入流有什么不同之处?下面我们来分析分析:管道用来把一个程序、线程和代码块的输出连接到另一个程序、线程和代码块的输入。java.io中提供了类PipedInputStream和PipedOutputStream作为管道输入/输出流。管道输入流作为一个通信管道的接收端,管道输出流则作为发送端。管道必须输输入输出作用,即在管道前,两者必须进行连接,可以在各自的构造方法中进行连接,或者使用各自的connect()方法进行连接。
实例:
测试类:
package com.yc.test.thread;
import java.io.IOException;
import java.io.PipedReader;
public class PipeTest {
public static void main(String[] args) {
PipedSender sender=new PipedSender();
PipedReciever receiver=new PipedReciever();
//连接管道
try {
sender.getPos().connect(receiver.getPis());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//启动线程
new Thread(sender).start();
new Thread(receiver).start();
}
}
管道输出流(用于进行数据传送)类PipedSender
package com.yc.test.thread;
import java.io.IOException;
import java.io.PipedOutputStream;
public class PipedSender implements Runnable{
private PipedOutputStream pos=null;
public PipedSender(){
this.pos=new PipedOutputStream();
}
public void run(){
String str="Hell!Amigo";
try {
this.pos.write(str.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
this.pos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public PipedOutputStream getPos() {
return pos;
}
public void setPos(PipedOutputStream pos) {
this.pos = pos;
}
}
管道输入流(用于接收数据)类PipedReceiver
package com.yc.test.thread;
import java.io.IOException;
import java.io.PipedInputStream;
public class PipedReciever implements Runnable{
private PipedInputStream pis=null;
public PipedReciever(){
this.pis=new PipedInputStream();
}
public void run(){
byte b[]=new byte[1024];
int len=0;
try {
len=this.pis.read(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
this.pis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("接受的内容为:"+new String(b,0,len));
}
public PipedInputStream getPis(){
return pis;
}
}
运行结果:
接受的内容为:Hell!Amigo
实例:
测试类:
package com.yc.test.thread;
import java.io.IOException;
import java.io.PipedReader;
public class PipeTest {
public static void main(String[] args) {
PipedSender sender=new PipedSender();
PipedReciever receiver=new PipedReciever();
//连接管道
try {
sender.getPos().connect(receiver.getPis());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//启动线程
new Thread(sender).start();
new Thread(receiver).start();
}
}
管道输出流(用于进行数据传送)类PipedSender
package com.yc.test.thread;
import java.io.IOException;
import java.io.PipedOutputStream;
public class PipedSender implements Runnable{
private PipedOutputStream pos=null;
public PipedSender(){
this.pos=new PipedOutputStream();
}
public void run(){
String str="Hell!Amigo";
try {
this.pos.write(str.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
this.pos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public PipedOutputStream getPos() {
return pos;
}
public void setPos(PipedOutputStream pos) {
this.pos = pos;
}
}
管道输入流(用于接收数据)类PipedReceiver
package com.yc.test.thread;
import java.io.IOException;
import java.io.PipedInputStream;
public class PipedReciever implements Runnable{
private PipedInputStream pis=null;
public PipedReciever(){
this.pis=new PipedInputStream();
}
public void run(){
byte b[]=new byte[1024];
int len=0;
try {
len=this.pis.read(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
this.pis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("接受的内容为:"+new String(b,0,len));
}
public PipedInputStream getPis(){
return pis;
}
}
运行结果:
接受的内容为:Hell!Amigo
相关文章推荐
- Java中==操作符与equals方法的区别
- Atitit.异常的设计原理与 策略处理 java 最佳实践 p93
- Atitit.异常的设计原理与 策略处理 java 最佳实践 p93
- Java版本的一些问题
- 二分查找java代码
- java10
- java9
- java8
- struts2-ajax-树状数据的输出
- java 第十天 面向对象
- java7
- java6
- java5
- java的苦恼
- java4
- jdk1.8.0_45源码解读——Set接口和AbstractSet抽象类的实现
- Hibernate中 初学者常见的问题
- java 第十天 包
- java synchronized详解
- hp实训之Java(一)