字節數組輸入流讀取操作


和文件的讀取操作類似,不同的是構造函數中是用 byte[]來初始化 ByteArrayInputStream

package com.machuang.io.others;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ByteArray {

    public static void main(String[] args) throws IOException {
        byteArrayRead();

    }

    
    public static void byteArrayRead() throws IOException {
        // 創造字節數組,(從服務器或者其他主機上傳來的 byte[])
        String msg = "和文件讀取操作一樣";        
        byte[] msgBytes = msg.getBytes();    // 待讀取的字節數組
        
        InputStream bis = new BufferedInputStream( new ByteArrayInputStream(msgBytes) );
        
        // byteBuf
        byte[] byteBuf = new byte[1024];
        int len = 0;
        
        // 讀取操作
        while(-1 != (len = bis.read(byteBuf))) {
            System.out.println(new String(byteBuf, 0, len));
        }
        
        bis.close();
    
    }
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM