标签:try catch ati length eth color 输出 void adr
(1)PipedInputStream与PipedOutputStream
(2)PipedReader与PipedWriter
(1)PipedInputStream与PipedOutputStream
package com.ming.thread.pipeinputoutput; import java.io.PipedInputStream; public class ReadData { public void readMethod(PipedInputStream input){ try { System.out.println("read :"); byte[] byteArray=new byte[20]; int readLength=input.read(byteArray); while(readLength!=-1){ String newData=new String(byteArray,0,readLength); System.out.print(newData); readLength=input.read(byteArray); } System.out.println(); input.close(); } catch (Exception e) { // TODO: handle exception } } }
package com.ming.thread.pipeinputoutput; import java.io.PipedOutputStream; public class WriteData { public void writeMethod(PipedOutputStream out){ try { System.out.println("write :"); for(int i=0;i<300;i++){ String outData="" +(i+1); out.write(outData.getBytes()); System.out.print(outData); } System.out.println(); out.close(); } catch (Exception e) { // TODO: handle exception } } }
package com.ming.thread.pipeinputoutput; import java.io.PipedOutputStream; public class ThreadWrite extends Thread { WriteData write; PipedOutputStream out; public ThreadWrite(WriteData write,PipedOutputStream out){ this.write=write; this.out=out; } public void run(){ write.writeMethod(out); } }
package com.ming.thread.pipeinputoutput; import java.io.PipedInputStream; public class ThreadRead extends Thread { ReadData read; PipedInputStream input; public ThreadRead(ReadData read,PipedInputStream input){ this.read=read; this.input=input; } public void run(){ read.readMethod(input); } }
package com.ming.thread.pipeinputoutput; import java.io.PipedInputStream; import java.io.PipedOutputStream; /** * 管道流的一个测试 * @author mingge * */ public class Run { public static void main(String[] args) { try { WriteData writeData=new WriteData(); ReadData readData=new ReadData(); PipedInputStream input=new PipedInputStream(); PipedOutputStream out=new PipedOutputStream(); out.connect(input); ThreadRead threadRead=new ThreadRead(readData, input); threadRead.start(); Thread.sleep(2000); ThreadWrite threadWrite=new ThreadWrite(writeData, out); threadWrite.start(); } catch (Exception e) { e.printStackTrace(); } } }
(2)PipedReader与PipedWriter
package com.ming.thread.pipereaderwriter; import java.io.PipedReader; public class ReadData { public void readMethod(PipedReader read){ try { System.out.println("read :"); char[] byteArray=new char[20]; int readLength=read.read(byteArray); while(readLength!=-1){ String newData=new String(byteArray, 0, readLength); System.out.print(newData); readLength=read.read(byteArray); } System.out.println(); read.close(); } catch (Exception e) { e.printStackTrace(); } } }
package com.ming.thread.pipereaderwriter; import java.io.PipedWriter; public class WriteData { public void writeMethod(PipedWriter out){ try { System.out.println("write :"); for(int i=0;i<300;i++){ String outData=""+(i+1); out.write(outData); System.out.print(outData); } System.out.println(); out.close(); } catch (Exception e) { e.printStackTrace(); } } }
package com.ming.thread.pipereaderwriter; import java.io.PipedReader; public class ThreadReader extends Thread { ReadData read; PipedReader input; public ThreadReader(ReadData read,PipedReader input){ this.read=read; this.input=input; } public void run(){ read.readMethod(input); } }
package com.ming.thread.pipereaderwriter; import java.io.PipedWriter; public class ThreadWrite extends Thread { WriteData write; PipedWriter out; public ThreadWrite(WriteData write,PipedWriter out){ this.write=write; this.out=out; } public void run(){ write.writeMethod(out); } }
package com.ming.thread.pipereaderwriter; import java.io.PipedReader; import java.io.PipedWriter; public class Run { public static void main(String[] args) { try { WriteData writeData=new WriteData(); ReadData readData=new ReadData(); PipedReader inputStream=new PipedReader(); PipedWriter outputStream=new PipedWriter(); outputStream.connect(inputStream); ThreadReader threadRead=new ThreadReader(readData, inputStream); threadRead.start(); Thread.sleep(2000); ThreadWrite threadWrite=new ThreadWrite(writeData, outputStream); threadWrite.start(); } catch (Exception e) { e.printStackTrace(); } } }
就是看代码就可以了...
标签:try catch ati length eth color 输出 void adr
原文地址:http://www.cnblogs.com/huzi007/p/7080393.html