天天看點

java _io_位元組數組輸出流

位元組數組輸出流,無需添加目的地,因為資料會被自動輸入記憶體的緩沖區,需通過

.toByteArray()或.toString()拿到資料

因為需要使用子類ByteArrayOutputStream的新方法,是以不能寫父類OutputStream對象

ByteArrayOutputStream os=new ByteArrayOutputStream();

因為資料寫入了緩沖區,是以需要通過.toByteArray()和.toString()手動拿取

步驟:

建立目的地位元組數組(用來存放從緩沖區拿來的資料): Byte[] last=null;

選擇流: ByteArrayOutputStream os;

編碼:字元串到位元組

操作:os.write(byte[],0,byte,length)寫入

擷取資料:last=os.toByteArray();

System.out.println(new String(last,0,last.length)//或last.length可替換成os.size()

解碼

}