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

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

創(chuàng)新互聯(lián)公司是工信部頒發(fā)資質(zhì)IDC服務(wù)器商,為用戶提供優(yōu)質(zhì)的成都服務(wù)器托管服務(wù)

Java可以用來干什么

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

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

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

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

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

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

2.寫入報(bào)錯(cuò)分析

在線程Sender中向管道流中寫入一個(gè)字符串,寫入后關(guān)閉該管道流;在線程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);
 
            //運(yùn)行子線程
            executorService.execute(sender);
            executorService.execute(reciever);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //等待子線程結(jié)束
        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. 今天是個(gè)好天氣。";
                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();
            }
        }
    }
}

注意,若管道流沒有關(guān)閉,則使用這種方法讀取管道中的信息會(huì)報(bào)錯(cuò):

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

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

上述內(nèi)容就是在java中寫入管道流上出現(xiàn)報(bào)錯(cuò)如何解決,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前標(biāo)題:在java中寫入管道流上出現(xiàn)報(bào)錯(cuò)如何解決
URL分享:http://bm7419.com/article14/jcsgde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作營銷型網(wǎng)站建設(shè)、企業(yè)建站標(biāo)簽優(yōu)化、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)