偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

使用 PipedInputStream 類與 PipedOutputStream 類學(xué)習(xí)管道流之間的通信

網(wǎng)絡(luò) 通信技術(shù)
在應(yīng)用程序中使用PipedInputStream類與PipedOutputStream類來創(chuàng)建管道之間的通信,一個PipedInputStream實例化對象和一個PipedOutputStream實例化對象進(jìn)行連接產(chǎn)生一個通信管道。

[[433836]]

大家好,我是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指定的管道輸出流。如下所示:

  1. 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指定的管道輸入流。如下所示:

  1. 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):

  1. import java.io.*; 
  2.  
  3. public class P22 { 
  4. public static void main(String[] args) throws Exception { 
  5. //創(chuàng)建PipedInputStream對象 
  6. final PipedInputStream in=new PipedInputStream(); 
  7. final PipedOutputStream out=new PipedOutputStream(); 
  8. //兩個類進(jìn)行連接 
  9. in.connect(out); 
  10. new Thread(new Runnable(){ 
  11.         public void run(){ 
  12.             BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
  13.             //通過鍵盤讀取數(shù)據(jù)寫入管道流 
  14.             PrintStream ps=new PrintStream(out); 
  15.             System.out.print(Thread.currentThread().getName()+"請輸入內(nèi)容:"); 
  16.             try { 
  17.                 ps.println(br.readLine()); 
  18.             } catch (IOException e) { 
  19.                 e.printStackTrace(); 
  20.             } 
  21.         } 
  22. },"發(fā)送數(shù)據(jù)的線程").start(); 
  23. new Thread(new Runnable(){ 
  24.         public void run(){ 
  25.             BufferedReader br=new BufferedReader(new InputStreamReader(in)); 
  26.             try { 
  27.                 System.out.println(Thread.currentThread().getName()+"收到的內(nèi)容:"+br.readLine()); 
  28.             } catch (IOException e) { 
  29.                 e.printStackTrace(); 
  30.             } 
  31.         } 
  32. },"接受數(shù)據(jù)的線程").start(); 
  33.   } 

運行的結(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í)交流公眾號。

 

責(zé)任編輯:武曉燕 來源: Java進(jìn)階學(xué)習(xí)交流
相關(guān)推薦

2011-03-10 09:07:47

liferayportlet

2012-12-24 14:40:54

iosjs

2011-07-07 17:48:36

PHP

2021-10-17 18:54:40

Python定義使用

2010-09-06 11:26:18

CSS偽類

2015-08-06 15:13:49

runtimeIOS開發(fā)

2010-01-06 13:44:22

JSON的類庫

2010-06-09 10:04:59

UML類圖

2010-06-10 16:09:28

路由選擇協(xié)議

2009-07-22 09:31:59

Scala類類層級Java類

2010-01-15 18:35:25

C++的類

2019-05-13 10:00:41

Linux進(jìn)程間通信命令

2024-04-02 11:34:09

成員對象封閉類C++

2023-11-02 18:05:55

Ray深度學(xué)習(xí)

2011-08-08 09:51:52

Cocoa 框架

2009-06-08 17:59:00

HibernateTemplate

2009-12-03 18:15:04

Linux

2019-06-25 10:53:06

AndroidFlutter通信

2011-08-31 13:22:37

PhoneGapAndroidjavascript

2012-12-21 09:19:29

Google GO
點贊
收藏

51CTO技術(shù)棧公眾號