詳解Java中ByteArray字節(jié)數(shù)組的輸入輸出流的用法
ByteArrayInputStream 介紹
ByteArrayInputStream 是字節(jié)數(shù)組輸入流。它繼承于InputStream。
它包含一個內(nèi)部緩沖區(qū),該緩沖區(qū)包含從流中讀取的字節(jié);通俗點說,它的內(nèi)部緩沖區(qū)就是一個字節(jié)數(shù)組,而ByteArrayInputStream本質(zhì)就是通過字節(jié)數(shù)組來實現(xiàn)的。
我們都知道,InputStream通過read()向外提供接口,供它們來讀取字節(jié)數(shù)據(jù);而ByteArrayInputStream 的內(nèi)部額外的定義了一個計數(shù)器,它被用來跟蹤 read() 方法要讀取的下一個字節(jié)。
示例代碼
關(guān)于ByteArrayInputStream中API的詳細用法,參考示例代碼(ByteArrayInputStreamTest.java):
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
/**
* ByteArrayInputStream 測試程序
*/
public class ByteArrayInputStreamTest {
private static final int LEN = 5;
// 對應(yīng)英文字母“abcddefghijklmnopqrsttuvwxyz”
private static final byte[] ArrayLetters = {
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A
};
public static void main(String[] args) {
String tmp = new String(ArrayLetters);
System.out.println("ArrayLetters="+tmp);
tesByteArrayInputStream() ;
}
/**
* ByteArrayInputStream的API測試函數(shù)
*/
private static void tesByteArrayInputStream() {
// 創(chuàng)建ByteArrayInputStream字節(jié)流,內(nèi)容是ArrayLetters數(shù)組
ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters);
// 從字節(jié)流中讀取5個字節(jié)
for (int i=0; i<LEN; i++) {
// 若能繼續(xù)讀取下一個字節(jié),則讀取下一個字節(jié)
if (bais.available() >= 0) {
// 讀取“字節(jié)流的下一個字節(jié)”
int tmp = bais.read();
System.out.printf("%d : 0x%s\n", i, Integer.toHexString(tmp));
}
}
// 若“該字節(jié)流”不支持標記功能,則直接退出
if (!bais.markSupported()) {
System.out.println("make not supported!");
return ;
}
// 標記“字節(jié)流中下一個被讀取的位置”。即--標記“0x66”,因為因為前面已經(jīng)讀取了5個字節(jié),所以下一個被讀取的位置是第6個字節(jié)”
// (01), ByteArrayInputStream類的mark(0)函數(shù)中的“參數(shù)0”是沒有實際意義的。
// (02), mark()與reset()是配套的,reset()會將“字節(jié)流中下一個被讀取的位置”重置為“mark()中所保存的位置”
bais.mark(0);
// 跳過5個字節(jié)。跳過5個字節(jié)后,字節(jié)流中下一個被讀取的值應(yīng)該是“0x6B”。
bais.skip(5);
// 從字節(jié)流中讀取5個數(shù)據(jù)。即讀取“0x6B, 0x6C, 0x6D, 0x6E, 0x6F”
byte[] buf = new byte[LEN];
bais.read(buf, 0, LEN);
// 將buf轉(zhuǎn)換為String字符串?!?x6B, 0x6C, 0x6D, 0x6E, 0x6F”對應(yīng)字符是“klmno”
String str1 = new String(buf);
System.out.printf("str1=%s\n", str1);
// 重置“字節(jié)流”:即,將“字節(jié)流中下一個被讀取的位置”重置到“mark()所標記的位置”,即0x66。
bais.reset();
// 從“重置后的字節(jié)流”中讀取5個字節(jié)到buf中。即讀取“0x66, 0x67, 0x68, 0x69, 0x6A”
bais.read(buf, 0, LEN);
// 將buf轉(zhuǎn)換為String字符串?!?x66, 0x67, 0x68, 0x69, 0x6A”對應(yīng)字符是“fghij”
String str2 = new String(buf);
System.out.printf("str2=%s\n", str2);
}
}
運行結(jié)果:
ArrayLetters=abcdefghijklmnopqrstuvwxyz 0 : 0x61 1 : 0x62 2 : 0x63 3 : 0x64 4 : 0x65 str1=klmno str2=fghij
結(jié)果說明:
(01) ArrayLetters 是字節(jié)數(shù)組。0x61對應(yīng)的ASCII碼值是a,0x62對應(yīng)的ASCII碼值是b,依次類推...
(02) ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters); 這句話是創(chuàng)建“字節(jié)流bais”,它的內(nèi)容就是ArrayLetters。
(03) for (int i=0; i<LEN; i++) ; 這個for循環(huán)的作用就是從字節(jié)流中讀取5個字節(jié)。每次調(diào)用bais.read()就從字節(jié)流中讀取一個字節(jié)。
(04) bais.mark(0); 這句話就是“設(shè)置字節(jié)流的標記”,此時標記的位置對應(yīng)的值是0x66。
(05) bais.skip(5); 這句話是跳過5個字節(jié)。跳過5個字節(jié)后,對應(yīng)的字節(jié)流中下一個被讀取的字節(jié)的值是0x6B。
(06) bais.read(buf, 0, LEN); 這句話是“從字節(jié)流中讀取LEN個數(shù)據(jù)寫入到buf中,0表示從buf的第0個位置開始寫入”。
(07) bais.reset(); 這句話是將“字節(jié)流中下一個被讀取的位置”重置到“mark()所標記的位置”,即0x66。
學完了ByteArrayInputStream輸入流。下面,我們學習與之對應(yīng)的輸出流ByteArrayOutputStream。
ByteArrayOutputStream 介紹
ByteArrayOutputStream 是字節(jié)數(shù)組輸出流。它繼承于OutputStream。
ByteArrayOutputStream 中的數(shù)據(jù)被寫入一個 byte 數(shù)組。緩沖區(qū)會隨著數(shù)據(jù)的不斷寫入而自動增長??墒褂?toByteArray() 和 toString() 獲取數(shù)據(jù)。
示例代碼
關(guān)于ByteArrayOutputStream中API的詳細用法,參考示例代碼(ByteArrayOutputStreamTest.java):
import java.io.IOException;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
/**
* ByteArrayOutputStream 測試程序
*
* @author skywang
*/
public class ByteArrayOutputStreamTest {
private static final int LEN = 5;
// 對應(yīng)英文字母“abcddefghijklmnopqrsttuvwxyz”
private static final byte[] ArrayLetters = {
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A
};
public static void main(String[] args) {
//String tmp = new String(ArrayLetters);
//System.out.println("ArrayLetters="+tmp);
tesByteArrayOutputStream() ;
}
/**
* ByteArrayOutputStream的API測試函數(shù)
*/
private static void tesByteArrayOutputStream() {
// 創(chuàng)建ByteArrayOutputStream字節(jié)流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 依次寫入“A”、“B”、“C”三個字母。0x41對應(yīng)A,0x42對應(yīng)B,0x43對應(yīng)C。
baos.write(0x41);
baos.write(0x42);
baos.write(0x43);
System.out.printf("baos=%s\n", baos);
// 將ArrayLetters數(shù)組中從“3”開始的后5個字節(jié)寫入到baos中。
// 即對應(yīng)寫入“0x64, 0x65, 0x66, 0x67, 0x68”,即“defgh”
baos.write(ArrayLetters, 3, 5);
System.out.printf("baos=%s\n", baos);
// 計算長度
int size = baos.size();
System.out.printf("size=%s\n", size);
// 轉(zhuǎn)換成byte[]數(shù)組
byte[] buf = baos.toByteArray();
String str = new String(buf);
System.out.printf("str=%s\n", str);
// 將baos寫入到另一個輸出流中
try {
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
baos.writeTo((OutputStream)baos2);
System.out.printf("baos2=%s\n", baos2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
運行結(jié)果:
baos=ABC baos=ABCdefgh size=8 str=ABCdefgh baos2=ABCdefgh
相關(guān)文章
關(guān)于SpringBoot創(chuàng)建存儲令牌的媒介類和過濾器的問題
這篇文章主要介紹了SpringBoot創(chuàng)建存儲令牌的媒介類和過濾器的問題,需要在配置文件中,添加JWT需要的密匙,過期時間和緩存過期時間,具體實例代碼參考下本文2021-09-09
詳解rabbitmq創(chuàng)建queue時arguments參數(shù)注釋
這篇文章主要介紹了rabbitmq創(chuàng)建queue時arguments參數(shù)注釋,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03
mybatisPlus條件構(gòu)造器常用方法小結(jié)
這篇文章主要介紹了mybatisPlus條件構(gòu)造器常用方法,首先是.select和其他條件,本文結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-10-10

