ByteArrayOutputStream
ByteArrayOutputStream: 可以捕获内存缓冲区的数据,转换成字节数组
ByteArrayInputStream: 可以将字节数组转化为输入流
|
综 合DataOutputStream&DataInputStream的作用和功能,与ByteArrayOutputStream和 ByteArrayInputSream使用将更方便.此时DataOutputStream&DataInputStream封闭了字节流,以 适当的形式读出了字节数组中的数据.如下所示:
public static void main(String[] args) throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
String name = "xxy";
int age = 84;
dout.writeUTF(name);
dout.writeInt(age);
byte[] buff = bout.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(buff);
DataInputStream dis = new DataInputStream(bin);
String newName = dis.readUTF();
int newAge = dis.readInt();
System.out.println(newName + ":" + newAge);
}