首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java IO之管道源

2012-08-24 
Java IO之管道流package com.chenzehe.test.ioimport java.io.PipedInputStreamimport java.io.PipedOut

Java IO之管道流

package com.chenzehe.test.io;import java.io.PipedInputStream;import java.io.PipedOutputStream;// 发送数据的类class Send implements Runnable {private PipedOutputStream output = null;public Send() {this.output = new PipedOutputStream();}public void run() {String helloworld = "hello world!";try {this.output.write(helloworld.getBytes());this.output.close();} catch (Exception e) {}}public PipedOutputStream getOutput() {return output;}public void setOutput(PipedOutputStream output) {this.output = output;}}// 接收数据的类class Receive implements Runnable {private PipedInputStream input = null;public Receive() {this.input = new PipedInputStream();}public void run() {byte[] b = new byte[1024];int len = 0;try {len = this.input.read(b);this.input.close();} catch (Exception e) {}System.out.println(new String(b, 0, len));}public PipedInputStream getInput() {return input;}public void setInput(PipedInputStream input) {this.input = input;}}public class PipedStreamDemo {public static void main(String[] args) throws Exception {Send send = new Send();Receive receive = new Receive();send.getOutput().connect(receive.getInput());// 进行管道连接new Thread(send).start();new Thread(receive).start();}}

热点排行