Java IO流操作(PipeInputStream、SequenceInputStream、BufferedInputStream)
一、PipeInputStream
1、介紹。
管道流的主要作用是可以進(jìn)行兩個(gè)線程間的通訊,分為管道輸入流(PipeOutputStream)和管道輸出流(PipeInputStream)。如果要想進(jìn)行管道輸出,則必須把輸出流連在輸入流之上
2、代碼。
private static final String Pre_Path = "G:\\項(xiàng)目測(cè)試\\io流文件測(cè)試\\"; public static void main(String[] args) { pipedStreamTest();//進(jìn)行兩個(gè)線程之間的通信 } //管道--進(jìn)行兩個(gè)線程之間的通信 private static void pipedStreamTest() { SendPipedThread sendThread = new SendPipedThread(); sendThread.start(); ReceivePipedThread receiveThread = new ReceivePipedThread(); receiveThread.start(); try { sendThread.outputStream.connect(receiveThread.inputStream); Thread.sleep(10000);//10S sendThread.stopThread(); receiveThread.stopThread(); } catch (Exception e) { e.printStackTrace(); } } //管道--每隔500ms,輸出當(dāng)前時(shí)間的字節(jié)數(shù)組 static class SendPipedThread extends Thread { PipedOutputStream outputStream = new PipedOutputStream();//輸出流 @Override public void run() { try { while (outputStream != null) { String writeStr = "時(shí)間:" + DateUtils.nowDateStr("yyyy-MM-dd hh:mm:ss") + " "; System.out.println("Send線程發(fā)送:" + writeStr); outputStream.write(writeStr.getBytes()); Thread.sleep(500); } } catch (Exception e) { e.printStackTrace(); } } void stopThread() { try { if (outputStream != null) { outputStream.close(); } } catch (Exception e) { e.printStackTrace(); } } } //管道--獲取連接管道的字節(jié)數(shù)組 static class ReceivePipedThread extends Thread { PipedInputStream inputStream = new PipedInputStream();//輸入流 @Override public void run() { try { while (inputStream != null) { byte[] readBytes = new byte[1024]; int len; while ((len = inputStream.read(readBytes)) != -1) { String readStr = new String(readBytes); System.out.println("Receive線程接收:" + readStr); } Thread.sleep(10); } } catch (Exception e) { e.printStackTrace(); } } void stopThread() { try { if (inputStream != null) { inputStream.close(); } } catch (Exception e) { e.printStackTrace(); } } }
3、結(jié)果。
Connected to the target VM, address: '127.0.0.1:58908', transport: 'socket'
Send線程發(fā)送:時(shí)間:2019-10-24 03:50:40
Send線程發(fā)送:時(shí)間:2019-10-24 03:50:40
Receive線程接收:時(shí)間:2019-10-24 03:50:40 時(shí)間:2019-10-24 03:50:40 Send線程發(fā)送:時(shí)間:2019-10-24 03:50:41
Send線程發(fā)送:時(shí)間:2019-10-24 03:50:41
Receive線程接收:時(shí)間:2019-10-24 03:50:41 時(shí)間:2019-10-24 03:50:41 Send線程發(fā)送:時(shí)間:2019-10-24 03:50:42
Send線程發(fā)送:時(shí)間:2019-10-24 03:50:42
Receive線程接收:時(shí)間:2019-10-24 03:50:42 時(shí)間:2019-10-24 03:50:42 Send線程發(fā)送:時(shí)間:2019-10-24 03:50:43
Send線程發(fā)送:時(shí)間:2019-10-24 03:50:43
Receive線程接收:時(shí)間:2019-10-24 03:50:43 時(shí)間:2019-10-24 03:50:43 Send線程發(fā)送:時(shí)間:2019-10-24 03:50:44
Send線程發(fā)送:時(shí)間:2019-10-24 03:50:44
Receive線程接收:時(shí)間:2019-10-24 03:50:44 時(shí)間:2019-10-24 03:50:44 Send線程發(fā)送:時(shí)間:2019-10-24 03:50:45
Send線程發(fā)送:時(shí)間:2019-10-24 03:50:45
Receive線程接收:時(shí)間:2019-10-24 03:50:45 時(shí)間:2019-10-24 03:50:45 Send線程發(fā)送:時(shí)間:2019-10-24 03:50:46
Send線程發(fā)送:時(shí)間:2019-10-24 03:50:46
Receive線程接收:時(shí)間:2019-10-24 03:50:46 時(shí)間:2019-10-24 03:50:46 Send線程發(fā)送:時(shí)間:2019-10-24 03:50:47
Send線程發(fā)送:時(shí)間:2019-10-24 03:50:47
Receive線程接收:時(shí)間:2019-10-24 03:50:47 時(shí)間:2019-10-24 03:50:47 Send線程發(fā)送:時(shí)間:2019-10-24 03:50:48
Send線程發(fā)送:時(shí)間:2019-10-24 03:50:48
Receive線程接收:時(shí)間:2019-10-24 03:50:48 時(shí)間:2019-10-24 03:50:48 Send線程發(fā)送:時(shí)間:2019-10-24 03:50:49
Send線程發(fā)送:時(shí)間:2019-10-24 03:50:49
java.io.IOException: Pipe closed
at java.io.PipedInputStream.read(PipedInputStream.java:307)
at java.io.PipedInputStream.read(PipedInputStream.java:377)
at java.io.InputStream.read(InputStream.java:101)
at com.zxj.reptile.test.IOStreamTest$ReceivePipedThread.run(IOStreamTest.java:211)
Send線程發(fā)送:時(shí)間:2019-10-24 03:50:50
java.io.IOException: Pipe closed
at java.io.PipedInputStream.checkStateForReceive(PipedInputStream.java:260)
at java.io.PipedInputStream.receive(PipedInputStream.java:226)
at java.io.PipedOutputStream.write(PipedOutputStream.java:149)
at java.io.OutputStream.write(OutputStream.java:75)
at com.zxj.reptile.test.IOStreamTest$SendPipedThread.run(IOStreamTest.java:182)
Disconnected from the target VM, address: '127.0.0.1:58908', transport: 'socket'
Process finished with exit code 0
二、SequenceInputStream
1、介紹。
SequenceInputStream 可以將兩個(gè)或多個(gè)其他 InputStream 合并為一個(gè)。 首先,SequenceInputStream 將讀取第一個(gè) InputStream 中的所有字節(jié),然后讀取第二個(gè) InputStream 中的所有字節(jié)。 這就是它被稱為 SequenceInputStream 的原因,因?yàn)?InputStream 實(shí)例是按順序讀取的。
2、代碼。
private static final String Pre_Path = "G:\\項(xiàng)目測(cè)試\\io流文件測(cè)試\\"; public static void main(String[] args) { sequenceStreamTest();//將多個(gè)文件復(fù)制到同一個(gè)文件中 }
//將多個(gè)文件復(fù)制到同一個(gè)文件中 private static void sequenceStreamTest() { InputStream inputStream = null;//輸入流 OutputStream outputStream = null;//輸出流 try { outputStream = new FileOutputStream(Pre_Path + "sequenceStream\\輸出文件.txt");//輸出流 Vector<InputStream> vector = new Vector<>(); vector.add(new FileInputStream(Pre_Path + "sequenceStream\\a.txt")); vector.add(new FileInputStream(Pre_Path + "sequenceStream\\b.txt")); vector.add(new FileInputStream(Pre_Path + "sequenceStream\\c.txt")); inputStream = new SequenceInputStream(vector.elements()); //從輸入流讀取0 到 255數(shù)量的字節(jié),并寫(xiě)入輸出流中 int readByte; while ((readByte = inputStream.read()) != -1) { outputStream.write(readByte); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (inputStream != null) { inputStream.close();//SequenceInputStream的close,會(huì)將里面的各個(gè)流全部關(guān)閉 } if (outputStream != null) { outputStream.close(); } } catch (Exception e) { e.printStackTrace(); } } }
3、測(cè)試用例。
4、結(jié)果。
三、BufferedInputStream
1、介紹。
BufferedInputStream繼承于FilterInputStream,提供緩沖輸入流功能。緩沖輸入流相對(duì)于普通輸入流的優(yōu)勢(shì)是,它提供了一個(gè)緩沖數(shù)組,每次調(diào)用read方法的時(shí)候,它首先嘗試從緩沖區(qū)里讀取數(shù)據(jù),若讀取失?。ň彌_區(qū)無(wú)可讀數(shù)據(jù)),則選擇從物理數(shù)據(jù)源(譬如文件)讀取新數(shù)據(jù)(這里會(huì)嘗試盡可能讀取多的字節(jié))放入到緩沖區(qū)中,最后再將緩沖區(qū)中的內(nèi)容部分或全部返回給用戶.由于從緩沖區(qū)里讀取數(shù)據(jù)遠(yuǎn)比直接從物理數(shù)據(jù)源(譬如文件)讀取速度快。
2、代碼。
private static final String Pre_Path = "G:\\項(xiàng)目測(cè)試\\io流文件測(cè)試\\"; public static void main(String[] args) { bufferedStreamTest();//文件的復(fù)制(字節(jié)緩沖流) }
//文件的復(fù)制(字節(jié)緩沖流) private static void bufferedStreamTest() { InputStream inputStream = null;//輸入流 OutputStream outputStream = null;//輸出流 try { inputStream = new BufferedInputStream(new FileInputStream(Pre_Path + "bufferStream\\test1.txt")); outputStream = new BufferedOutputStream(new FileOutputStream(Pre_Path + "bufferStream\\test2.txt")); //從輸入流讀取0 到 255數(shù)量的字節(jié),并寫(xiě)入輸出流中 byte[] readBytes = new byte[1024]; int len; while ((len = inputStream.read(readBytes)) != -1) { System.out.println("讀取到的字符:\n" + new String(readBytes)); outputStream.write(readBytes, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } } catch (Exception e) { e.printStackTrace(); } } }
3、測(cè)試用例。
4、結(jié)果。
到此這篇關(guān)于Java IO流操作(PipeInputStream、SequenceInputStream、BufferedInputStream)的文章就介紹到這了,更多相關(guān)Java IO流操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解析Java和Eclipse中加載本地庫(kù)(.dll文件)的詳細(xì)說(shuō)明
本篇文章是對(duì)Java和Eclipse中加載本地庫(kù)(.dll文件)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05使用java8 API遍歷過(guò)濾文件目錄及子目錄和隱藏文件示例詳解
這篇文章主要介紹了使用java8API遍歷過(guò)濾文件目錄及子目錄及隱藏文件示例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07JAVA?biginteger類bigdecimal類的使用示例學(xué)習(xí)
這篇文章主要為大家介紹了JAVA?biginteger類bigdecimal類的使用示例學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07java Disruptor構(gòu)建高性能內(nèi)存隊(duì)列使用詳解
這篇文章主要為大家介紹了java Disruptor構(gòu)建高性能內(nèi)存隊(duì)列使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12關(guān)于Lombok @Data注解:簡(jiǎn)化Java代碼的魔法棒
Lombok庫(kù)通過(guò)@Data注解自動(dòng)生成常見(jiàn)的樣板代碼如getter、setter、toString等,極大減少代碼量,提高開(kāi)發(fā)效率,@Data注解集成了@ToString、@EqualsAndHashCode、@Getter、@Setter、@RequiredArgsConstructor等注解的功能2024-10-10idea2020.1最新版永久破解/pycharm也可用(步驟詳解)
這篇文章主要介紹了idea2020.1最新版永久破解/pycharm也可用,本文給大家分享簡(jiǎn)單實(shí)現(xiàn)步驟,通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04