使用 PipedInputStream 類與 PipedOutputStream 類學(xué)習(xí)管道流之間的通信
大家好,我是Java進(jìn)階者。
前言
在應(yīng)用程序中使用PipedInputStream類與PipedOutputStream類來創(chuàng)建管道之間的通信,一個PipedInputStream實例化對象和一個PipedOutputStream實例化對象進(jìn)行連接產(chǎn)生一個通信管道。PipedInputStream用來從管道中讀取寫入的數(shù)據(jù),PipedOutputStream是向管道中寫入數(shù)據(jù)。通過線程之間的通信使用PipedInputStream類與PipedOutputStream類。使用PipedInputStream類與PipedOutputStream類學(xué)習(xí)管道流之間的通信。接下來小編帶大家一起來學(xué)習(xí)!
一、PipedInputStream類
1.PipedInputStream類管道輸入流,它是可以連接管道輸出流,管道輸入流提供了要寫入管道輸出流的所有數(shù)據(jù)的字節(jié)。
2.PipedInputStream類構(gòu)造方法:
(1)public PipedInputStream():創(chuàng)建管道輸入流對象,這個對象它尚未連接。
(2)public PipedInputStream(PipedOutputStream out):創(chuàng)建一個管道輸入流,它被連接到參數(shù)out指定的管道輸出流。如下所示:
- PipedInputStream pis=new PipedInputStream();
3.PipedInputStream類方法有:
(1)int read():讀取數(shù)據(jù)的下一個字節(jié)。
(2)int read(byte []b,int off,int len):讀取len個字節(jié)數(shù)據(jù)到一個字節(jié)數(shù)組,off參數(shù)表示偏移量,len表示讀取數(shù)據(jù)的長度。
(3)void receive(int b):接受一個字節(jié)的數(shù)據(jù)。
(4)void connect(PipedOutputStream src):表示管道輸入流連接到管道輸出流src
(5)int available():表示沒有什么阻礙從輸入流中讀取字節(jié)數(shù)。
(6)void close():表示關(guān)閉流。
二、PipedOutputStream類
1.PipedOutputStream類是管道輸出流,它是可以用于向管道中寫入數(shù)據(jù)。
2.PipedOutputStream類構(gòu)造方法:
(1)public PipedOutputStream():創(chuàng)建管道輸出流對象,這個對象它尚未連接。
(2)public PipedOutputStream(PipedInputStream in):創(chuàng)建一個管道輸出流,它被連接到參數(shù)in指定的管道輸入流。如下所示:
- PipedOutputStream pos=new PipedOutputStream();
3.PipedOutputStream類方法有:
(1)void close():表示關(guān)閉流。
(2)void connect(PipedInputStream snk):表示管道輸出流連接到管道輸入流中。
(3)void flush():刷新輸出流并強(qiáng)制的寫出任何緩沖的輸出字節(jié)。
(4)void write(int b):寫入指定字節(jié)到管道輸出流。
(5)void write(byte [] b,int off,int len):寫入len個字節(jié)的指定字節(jié)數(shù)組從偏移量off開始到管道輸出流。
三、使用PipedInputStream類與PipedOutputStream類學(xué)習(xí)管道流之間的通信案例
1.代碼實現(xiàn):
- import java.io.*;
- public class P22 {
- public static void main(String[] args) throws Exception {
- //創(chuàng)建PipedInputStream對象
- final PipedInputStream in=new PipedInputStream();
- final PipedOutputStream out=new PipedOutputStream();
- //兩個類進(jìn)行連接
- in.connect(out);
- new Thread(new Runnable(){
- public void run(){
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- //通過鍵盤讀取數(shù)據(jù)寫入管道流
- PrintStream ps=new PrintStream(out);
- System.out.print(Thread.currentThread().getName()+"請輸入內(nèi)容:");
- try {
- ps.println(br.readLine());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- },"發(fā)送數(shù)據(jù)的線程").start();
- new Thread(new Runnable(){
- public void run(){
- BufferedReader br=new BufferedReader(new InputStreamReader(in));
- try {
- System.out.println(Thread.currentThread().getName()+"收到的內(nèi)容:"+br.readLine());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- },"接受數(shù)據(jù)的線程").start();
- }
- }
運行的結(jié)果如下圖所示:
上面代碼中使用一個PipedInputStream對象和一個PipedOutputStream對象進(jìn)行連接產(chǎn)生一個通信管道。,寫兩個線程,一個線程用于鍵盤輸入的數(shù)據(jù)管道輸出流,另一個線程用來管道讀取寫入的數(shù)據(jù)。使用這兩個類來實現(xiàn)線程之間的通信。
四、總結(jié)
本文主要介紹了PipedInputStream類、PipedOutputStream類、使用PipedInputStream類與PipedOutputStream類學(xué)習(xí)管道流之間的通信。PipedInputStream類管道輸入流,它是可以連接管道輸出流,管道輸入流提供了要寫入管道輸出流的所有數(shù)據(jù)的字節(jié)。PipedInputStream介紹了它的構(gòu)造方法和方法。PipedOutputStream類是管道輸出流,它是可以用于向管道中寫入數(shù)據(jù)。PipedOutputStream介紹了它的構(gòu)造方法和方法。使用PipedInputStream類與PipedOutputStream類學(xué)習(xí)管道流之間的通信。希望大家通過本文的學(xué)習(xí),對你有所幫助!
本文轉(zhuǎn)載自微信公眾號「Java進(jìn)階學(xué)習(xí)交流」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系Java進(jìn)階學(xué)習(xí)交流公眾號。