06java应用技术3解读.ppt

06java应用技术3解读

* package cn.java.window; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; public class PipedOutputStreamDemo { /** * @param args */ public static void main(String[] args) { Person p = new Person(); Spook s = new Spook(); try { p.getPos().connect(s.getPis()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } new Thread(p).start(); new Thread(s).start(); } } //发送端 class Person implements Runnable{ private PipedOutputStream pos= null; public Person(){ pos = new PipedOutputStream(); } public void run() { String l = wo shi haoren; try { pos.write(l.getBytes()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { pos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public PipedOutputStream getPos(){ return pos; } } //接收端 class Spook implements Runnable{ PipedInputStream pis = null; /** * @param pis */ public Spook() { this.pis = new PipedInputStream(); } public void run() { byte[] b = new byte[1024]; int length = 0; try { length = pis.read(b); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { pis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.print(new String(b,0,length)); } public PipedInputStream getPis(){ return pis; } } package cn.java.io; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class SystemInDemo { public static void main(String[] args) throws IOException { InputStream in = System.in; //读取键盘输入数据 //方式一:(只能接受个数限制的输入) /*byte[] b = new byte[1000]; System.out.println(in:); int l = in.read(b); System.out.print(new String(b,0,l));

文档评论(0)

1亿VIP精品文档

相关文档