您好,登錄后才能下訂單哦!
package test; import java.io.PipedInputStream; import java.io.PipedOutputStream; public class PipeTest { public static void main(String[] args) throws Exception { PipedOutputStream pos = new PipedOutputStream(); PipedInputStream pis = new PipedInputStream(); //鏈接 pis.connect(pos); //寫線程 InThread it = new InThread(pos,pis); //讀線程 OutThread ot = new OutThread(pos,pis); it.start(); ot.start(); Thread.sleep(1000); } } class InThread extends Thread{ PipedOutputStream pos = null; PipedInputStream pis = null ; InThread(PipedOutputStream pos,PipedInputStream pis ){ this.pos = pos; this.pis = pis; } public void run() { try { //寫入數據 byte[] b = new String("this is a test !").getBytes(); pos.write(b); //關閉鏈接,此處必須關閉,不然會包異常 pos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class OutThread extends Thread{ PipedInputStream pis = null ; PipedOutputStream pos = null; OutThread(PipedOutputStream pos,PipedInputStream pis){ this.pis = pis; this.pos = pos; } public void run() { //讀取數據 String m = ""; byte[] b = new byte[1024]; try { int len ; len = pis.read(b); m = m+ new String(b); while(len!=-1) { len = pis.read(b); m = m+ new String(b); } //關閉資源 pis.close(); System.out.println(m); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。