ByteArrayOutputStream和ByteArrayInputStream详解
生活随笔
收集整理的這篇文章主要介紹了
ByteArrayOutputStream和ByteArrayInputStream详解
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
ByteArrayOutputStream類是在創(chuàng)建它的實例時,程序內(nèi)部創(chuàng)建一個byte型別數(shù)組的緩沖區(qū),然后利用ByteArrayOutputStream和ByteArrayInputStream的實例向數(shù)組中寫入或讀出byte型數(shù)據(jù)。在網(wǎng)絡(luò)傳輸中我們往往要傳輸很多變量,我們可以利用ByteArrayOutputStream把所有的變量收集到一起,然后一次性把數(shù)據(jù)發(fā)送出去。具體用法如下:?
ByteArrayOutputStream:??? 可以捕獲內(nèi)存緩沖區(qū)的數(shù)據(jù),轉(zhuǎn)換成字節(jié)數(shù)組。?
ByteArrayInputStream: 可以將字節(jié)數(shù)組轉(zhuǎn)化為輸入流?
1 import java.io.*;?
2? 3? public class test {?
4?? public static void main(String[] args) {?
5?? int a=0;?
6?? int b=1;?
7?? int c=2;?
8?? ByteArrayOutputStream bout = new ByteArrayOutputStream();?
9?? bout.write(a); //需要注意的是write只能是0-255之間的數(shù)字,否則超出byte的表示范圍,會丟失信息。如果a=257, 寫入bout的只是1
10?? bout.write(b);?
11?? bout.write(c);?
12?? byte[] buff = bout.toByteArray();?
13?? for(int i=0; i<buff.length; i++)?
14??? System.out.println(buff[i]);?
15?? System.out.println("***********************");?
16?? ByteArrayInputStream bin = new ByteArrayInputStream(buff);?
17??? while((b=bin.read())!=-1) {?
18??? System.out.println(b);?
19?? }?
20? }?
21 }?
綜合DataOutputStream&DataInputStream的作用和功能,與ByteArrayOutputStream和ByteArrayInputSream使用將更方便.此時DataOutputStream&DataInputStream封閉了字節(jié)流,以適當?shù)男问阶x出了字節(jié)數(shù)組中的數(shù)據(jù).如下所示:?
1 import java.io.*;?
2?
3? public class test {?
4?? public static void main(String[] args)throws IOException {?
5?? ByteArrayOutputStream bout = new ByteArrayOutputStream();?
6?? DataOutputStream dout = new DataOutputStream(bout);?
7?? String name = "xxy";?
8?? int age = 84;?
9?? dout.writeUTF(name);?
10?? dout.writeInt(age);?
11?? byte[] buff = bout.toByteArray();?
12?? ByteArrayInputStream bin = new ByteArrayInputStream(buff);?
13?? DataInputStream dis = new DataInputStream(bin);?
14?? String newName = dis.readUTF();?
15?? int newAge = dis.readInt();?
16?? System.out.println(newName+":"+newAge);?
17? }?
18 }?
字節(jié)數(shù)組流:?
ByteArrayOutputStream:??? 可以捕獲內(nèi)存緩沖區(qū)的數(shù)據(jù),轉(zhuǎn)換成字節(jié)數(shù)組。?
ByteArrayoutputStream bout=new ByteArrayOutputStream();?
bout.write(int a);? bout.write(int b);? bout.write(int c);?
byte[] buf=bout.toByteArray();//獲取內(nèi)存緩沖中的數(shù)據(jù)?
for(int i=0;i<=buf.length;i++)?
{?
? System.out.println(buf[i]);?
}?
bout.close();?
注:通過調(diào)用reset()方法可以重新定位。?
ByteArrayInputStream: 可以將字節(jié)數(shù)組轉(zhuǎn)化為輸入流?
ByteArrayInputStream bin=new ByteArrayInputStream(byte[] buf);?
int data=0;?
while( (b=bin.read())!=-1)?
{?
? System.out.println(b);?
}?
bin.close();?
與DataOutputStream&DataInputStream聯(lián)合使用:?
ByteArrayOutputStream bout=new ByteArrayOutputStream();?
DataOutputStream dos=new DataOutputStream(bout);?
String name="suntao";?
int age=19;?
dos.writeUTF(name);?
dos.writeInt(age);?
byte[] buf=bout.toByteArray();//獲取內(nèi)存緩沖區(qū)中的數(shù)據(jù)?
dos.close();?
bout.close();?
ByteArrayInputStream bin=new ByteArrayInputStream(byte[] buf);?
DataInputStream dis=new DataInputStream(bin);?
String name=dis.readUTF();//從字節(jié)數(shù)組中讀取?
int age=dis.readInt();?
dis.close();?
bin.close();?
注:? DataInputStream&DataOutputStream還可以與FileInputStream&FileOutputStream?
聯(lián)合使用。?
其中:?
DataInputStream&DataOutputStream關(guān)心如何將數(shù)據(jù)從高層次的形式轉(zhuǎn)化成低層次的形式.?
FileInputStream&FileOutputStream關(guān)心如何操作存儲單元以接受和產(chǎn)生數(shù)據(jù)。
ByteArrayOutputStream:??? 可以捕獲內(nèi)存緩沖區(qū)的數(shù)據(jù),轉(zhuǎn)換成字節(jié)數(shù)組。?
ByteArrayInputStream: 可以將字節(jié)數(shù)組轉(zhuǎn)化為輸入流?
1 import java.io.*;?
2? 3? public class test {?
4?? public static void main(String[] args) {?
5?? int a=0;?
6?? int b=1;?
7?? int c=2;?
8?? ByteArrayOutputStream bout = new ByteArrayOutputStream();?
9?? bout.write(a); //需要注意的是write只能是0-255之間的數(shù)字,否則超出byte的表示范圍,會丟失信息。如果a=257, 寫入bout的只是1
10?? bout.write(b);?
11?? bout.write(c);?
12?? byte[] buff = bout.toByteArray();?
13?? for(int i=0; i<buff.length; i++)?
14??? System.out.println(buff[i]);?
15?? System.out.println("***********************");?
16?? ByteArrayInputStream bin = new ByteArrayInputStream(buff);?
17??? while((b=bin.read())!=-1) {?
18??? System.out.println(b);?
19?? }?
20? }?
21 }?
22?
ByteArrayOutputStream在內(nèi)部會建立一個byte數(shù)組,調(diào)用write操作就是把字節(jié)寫入這個數(shù)組,調(diào)用toByteArray會將內(nèi)部byte數(shù)組的內(nèi)容復(fù)制到一個新的byte數(shù)組中并返回新的數(shù)組
要使用ByteArrayInoutStream, 必須將一個byte數(shù)組傳入,然后就是以操作流的方式操作這個數(shù)組,感覺用處不大
綜合DataOutputStream&DataInputStream的作用和功能,與ByteArrayOutputStream和ByteArrayInputSream使用將更方便.此時DataOutputStream&DataInputStream封閉了字節(jié)流,以適當?shù)男问阶x出了字節(jié)數(shù)組中的數(shù)據(jù).如下所示:?
1 import java.io.*;?
2?
3? public class test {?
4?? public static void main(String[] args)throws IOException {?
5?? ByteArrayOutputStream bout = new ByteArrayOutputStream();?
6?? DataOutputStream dout = new DataOutputStream(bout);?
7?? String name = "xxy";?
8?? int age = 84;?
9?? dout.writeUTF(name);?
10?? dout.writeInt(age);?
11?? byte[] buff = bout.toByteArray();?
12?? ByteArrayInputStream bin = new ByteArrayInputStream(buff);?
13?? DataInputStream dis = new DataInputStream(bin);?
14?? String newName = dis.readUTF();?
15?? int newAge = dis.readInt();?
16?? System.out.println(newName+":"+newAge);?
17? }?
18 }?
字節(jié)數(shù)組流:?
ByteArrayOutputStream:??? 可以捕獲內(nèi)存緩沖區(qū)的數(shù)據(jù),轉(zhuǎn)換成字節(jié)數(shù)組。?
ByteArrayoutputStream bout=new ByteArrayOutputStream();?
bout.write(int a);? bout.write(int b);? bout.write(int c);?
byte[] buf=bout.toByteArray();//獲取內(nèi)存緩沖中的數(shù)據(jù)?
for(int i=0;i<=buf.length;i++)?
{?
? System.out.println(buf[i]);?
}?
bout.close();?
注:通過調(diào)用reset()方法可以重新定位。?
ByteArrayInputStream: 可以將字節(jié)數(shù)組轉(zhuǎn)化為輸入流?
ByteArrayInputStream bin=new ByteArrayInputStream(byte[] buf);?
int data=0;?
while( (b=bin.read())!=-1)?
{?
? System.out.println(b);?
}?
bin.close();?
與DataOutputStream&DataInputStream聯(lián)合使用:?
ByteArrayOutputStream bout=new ByteArrayOutputStream();?
DataOutputStream dos=new DataOutputStream(bout);?
String name="suntao";?
int age=19;?
dos.writeUTF(name);?
dos.writeInt(age);?
byte[] buf=bout.toByteArray();//獲取內(nèi)存緩沖區(qū)中的數(shù)據(jù)?
dos.close();?
bout.close();?
ByteArrayInputStream bin=new ByteArrayInputStream(byte[] buf);?
DataInputStream dis=new DataInputStream(bin);?
String name=dis.readUTF();//從字節(jié)數(shù)組中讀取?
int age=dis.readInt();?
dis.close();?
bin.close();?
注:? DataInputStream&DataOutputStream還可以與FileInputStream&FileOutputStream?
聯(lián)合使用。?
其中:?
DataInputStream&DataOutputStream關(guān)心如何將數(shù)據(jù)從高層次的形式轉(zhuǎn)化成低層次的形式.?
FileInputStream&FileOutputStream關(guān)心如何操作存儲單元以接受和產(chǎn)生數(shù)據(jù)。
總結(jié)
以上是生活随笔為你收集整理的ByteArrayOutputStream和ByteArrayInputStream详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DataInputStream
- 下一篇: java网络编程Socket中SO_LI