组byte[]数组
String sendHead ="<?xml version="+"\""+"1.0"+"\""+" encoding="+"\""+"UTF-8"+"\""+"?><head></head>";
String sendData ="abcasdasdagagfahahahahahhaa";
byte[] headByte = sendHead.getBytes("UTF-8");
byte[] bodyByte = sendData.getBytes("UTF-8");
ByteArrayOutputStream baos = new ByteArrayOutputStream();//字符数组输出留
DataOutputStream dout = new DataOutputStream(baos);//管道输出流
dout.writeShort(headByte.length); //1次写2个字节,将short写入byte数组
dout.writeInt(bodyByte.length); //1次写4个字节,将int写入byte数组
dout.writeByte(0); //1次写1个字节,将byte写入byte数组
dout.write(headByte); //写入数据
dout.write(bodyByte);
dout.flush(); //强制输出,清空缓存
byte[] returndata = baos.toByteArray();
读取byte[]数组内容
byte[] bytes="123456789".getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
DataInputStream dis = new DataInputStream(bais);
System.out.println(dis.readByte()); //一次读1个字节,读4个字节请循环
System.out.println(dis.readShort());//一次读2个字节,并转化成Short
System.out.println(dis.readInt());//一次读4个字节,并转化成Int