溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

在java中寫入管道流上出現(xiàn)報錯如何解決

發(fā)布時間:2021-04-21 16:55:43 來源:億速云 閱讀:213 作者:Leah 欄目:編程語言

本篇文章為大家展示了在java中寫入管道流上出現(xiàn)報錯如何解決,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

Java可以用來干什么

Java主要應用于:1. web開發(fā);2. Android開發(fā);3. 客戶端開發(fā);4. 網(wǎng)頁開發(fā);5. 企業(yè)級應用開發(fā);6. Java大數(shù)據(jù)開發(fā);7.游戲開發(fā)等。

1.創(chuàng)建和連接管道兩端的兩種方法

(1)創(chuàng)建管道輸入和輸出流并連接它們。它使用connect方法連接兩個流。

PipedInputStream pis  = new PipedInputStream(); 
PipedOutputStream pos  = new PipedOutputStream(); 
pis.connect(pos); /* Connect  the   two  ends  */

(2)創(chuàng)建管道輸入和輸出流并連接它們。它通過將輸入管道流傳遞到輸出流構造器來連接兩個流。

PipedInputStream pis  = new PipedInputStream(); 
PipedOutputStream pos  = new PipedOutputStream(pis);

2.寫入報錯分析

在線程Sender中向管道流中寫入一個字符串,寫入后關閉該管道流;在線程Reciever中讀取該字符串。

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
 
public class PipedStreamExam1 {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
 
        try {
            //創(chuàng)建管道流
            PipedOutputStream pos = new PipedOutputStream();
            PipedInputStream pis = new PipedInputStream(pos);
 
            //創(chuàng)建線程對象
            Sender sender = new Sender(pos);
            Reciever reciever = new Reciever(pis);
 
            //運行子線程
            executorService.execute(sender);
            executorService.execute(reciever);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //等待子線程結束
        executorService.shutdown();
        try {
            executorService.awaitTermination(1, TimeUnit.DAYS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
 
    static class Sender extends Thread {
        private PipedOutputStream pos;
 
        public Sender(PipedOutputStream pos) {
            super();
            this.pos = pos;
        }
 
        @Override
        public void run() {
            try {
                String s = "This is a good day. 今天是個好天氣。";
                System.out.println("Sender:" + s);
                byte[] buf = s.getBytes();
                pos.write(buf, 0, buf.length);
                pos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    static class Reciever extends Thread {
        private PipedInputStream pis;
 
        public Reciever(PipedInputStream pis) {
            super();
            this.pis = pis;
        }
 
        @Override
        public void run() {
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                int len = 0;
                while ((len = pis.read(buf)) != -1) {
                    baos.write(buf, 0, len);
                }
                byte[] result = baos.toByteArray();
                String s = new String(result, 0, result.length);
                System.out.println("Reciever:" + s);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

注意,若管道流沒有關閉,則使用這種方法讀取管道中的信息會報錯:

while ((len = pis.read(buf)) != -1) {
    baos.write(buf, 0, len);
}

錯誤代碼為java.io.IOException: Write end dead;發(fā)生這個錯誤的原因在于,由于管道未關閉,所以read語句不會讀到-1,因此PipedInputStream會持續(xù)從管道中讀取數(shù)據(jù),但是因為Sender線程已經(jīng)結束,所以會拋出“Write end dead”異常。

上述內容就是在java中寫入管道流上出現(xiàn)報錯如何解決,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI