Java IO流操作(PipeInputStream、SequenceInputStream、BufferedInputStream)
一、PipeInputStream
1、介紹。
管道流的主要作用是可以進行兩個線程間的通訊,分為管道輸入流(PipeOutputStream)和管道輸出流(PipeInputStream)。如果要想進行管道輸出,則必須把輸出流連在輸入流之上
2、代碼。
private static final String Pre_Path = "G:\\項目測試\\io流文件測試\\";
public static void main(String[] args) {
pipedStreamTest();//進行兩個線程之間的通信
}
//管道--進行兩個線程之間的通信
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,輸出當前時間的字節(jié)數(shù)組
static class SendPipedThread extends Thread {
PipedOutputStream outputStream = new PipedOutputStream();//輸出流
@Override
public void run() {
try {
while (outputStream != null) {
String writeStr = "時間:" + 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ā)送:時間:2019-10-24 03:50:40
Send線程發(fā)送:時間:2019-10-24 03:50:40
Receive線程接收:時間:2019-10-24 03:50:40 時間:2019-10-24 03:50:40 Send線程發(fā)送:時間:2019-10-24 03:50:41
Send線程發(fā)送:時間:2019-10-24 03:50:41
Receive線程接收:時間:2019-10-24 03:50:41 時間:2019-10-24 03:50:41 Send線程發(fā)送:時間:2019-10-24 03:50:42
Send線程發(fā)送:時間:2019-10-24 03:50:42
Receive線程接收:時間:2019-10-24 03:50:42 時間:2019-10-24 03:50:42 Send線程發(fā)送:時間:2019-10-24 03:50:43
Send線程發(fā)送:時間:2019-10-24 03:50:43
Receive線程接收:時間:2019-10-24 03:50:43 時間:2019-10-24 03:50:43 Send線程發(fā)送:時間:2019-10-24 03:50:44
Send線程發(fā)送:時間:2019-10-24 03:50:44
Receive線程接收:時間:2019-10-24 03:50:44 時間:2019-10-24 03:50:44 Send線程發(fā)送:時間:2019-10-24 03:50:45
Send線程發(fā)送:時間:2019-10-24 03:50:45
Receive線程接收:時間:2019-10-24 03:50:45 時間:2019-10-24 03:50:45 Send線程發(fā)送:時間:2019-10-24 03:50:46
Send線程發(fā)送:時間:2019-10-24 03:50:46
Receive線程接收:時間:2019-10-24 03:50:46 時間:2019-10-24 03:50:46 Send線程發(fā)送:時間:2019-10-24 03:50:47
Send線程發(fā)送:時間:2019-10-24 03:50:47
Receive線程接收:時間:2019-10-24 03:50:47 時間:2019-10-24 03:50:47 Send線程發(fā)送:時間:2019-10-24 03:50:48
Send線程發(fā)送:時間:2019-10-24 03:50:48
Receive線程接收:時間:2019-10-24 03:50:48 時間:2019-10-24 03:50:48 Send線程發(fā)送:時間:2019-10-24 03:50:49
Send線程發(fā)送:時間: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ā)送:時間: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 可以將兩個或多個其他 InputStream 合并為一個。 首先,SequenceInputStream 將讀取第一個 InputStream 中的所有字節(jié),然后讀取第二個 InputStream 中的所有字節(jié)。 這就是它被稱為 SequenceInputStream 的原因,因為 InputStream 實例是按順序讀取的。
2、代碼。
private static final String Pre_Path = "G:\\項目測試\\io流文件測試\\";
public static void main(String[] args) {
sequenceStreamTest();//將多個文件復制到同一個文件中
}//將多個文件復制到同一個文件中
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é),并寫入輸出流中
int readByte;
while ((readByte = inputStream.read()) != -1) {
outputStream.write(readByte);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();//SequenceInputStream的close,會將里面的各個流全部關閉
}
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}3、測試用例。

4、結(jié)果。

三、BufferedInputStream
1、介紹。
BufferedInputStream繼承于FilterInputStream,提供緩沖輸入流功能。緩沖輸入流相對于普通輸入流的優(yōu)勢是,它提供了一個緩沖數(shù)組,每次調(diào)用read方法的時候,它首先嘗試從緩沖區(qū)里讀取數(shù)據(jù),若讀取失?。ň彌_區(qū)無可讀數(shù)據(jù)),則選擇從物理數(shù)據(jù)源(譬如文件)讀取新數(shù)據(jù)(這里會嘗試盡可能讀取多的字節(jié))放入到緩沖區(qū)中,最后再將緩沖區(qū)中的內(nèi)容部分或全部返回給用戶.由于從緩沖區(qū)里讀取數(shù)據(jù)遠比直接從物理數(shù)據(jù)源(譬如文件)讀取速度快。
2、代碼。
private static final String Pre_Path = "G:\\項目測試\\io流文件測試\\";
public static void main(String[] args) {
bufferedStreamTest();//文件的復制(字節(jié)緩沖流)
}//文件的復制(字節(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é),并寫入輸出流中
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、測試用例。

4、結(jié)果。

到此這篇關于Java IO流操作(PipeInputStream、SequenceInputStream、BufferedInputStream)的文章就介紹到這了,更多相關Java IO流操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java實現(xiàn)Redisson分布式鎖的10大陷阱與避坑指南
本文介紹了Redisson分布式鎖的基礎用法與高級特性,包括基本鎖操作、公平鎖、讀寫鎖、聯(lián)鎖和紅鎖等實現(xiàn)方式,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧2026-04-04
使用log4j2自定義配置文件位置和文件名(附log4j2.xml配置實例)
這篇文章主要介紹了使用log4j2自定義配置文件位置和文件名(附log4j2.xml配置實例),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
解決Mybatis 大數(shù)據(jù)量的批量insert問題
這篇文章主要介紹了解決Mybatis 大數(shù)據(jù)量的批量insert問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
java出現(xiàn)no XXX in java.library.path的解決及eclipse配
這篇文章主要介紹了java出現(xiàn)no XXX in java.library.path的解決及eclipse配置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
SpringBoot2使用WebFlux函數(shù)式編程的方法
這篇文章主要介紹了SpringBoot2使用WebFlux函數(shù)式編程的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08

