字节数组操作 ByteArrayOutputStream bos=new ByteArrayOutputStream(); DataOutputStream dos=new DataOutputStream(bos); byte records[]=null; String r=""; try { dos.writeInt(2); //记录个数 //dos.writeInt(2); //任务ID dos.writeInt(0); //任务类型 dos.writeUTF("松鼠"); //名称 dos.writeInt(1); //对话次数/物品件数/杀怪数 //dos.writeInt(3); //任务ID dos.writeInt(2); //任务类型 dos.writeUTF("王婆婆"); //名称 dos.writeInt(1); //对话次数/物品件数/杀怪数 records=bos.toByteArray(); r=bos.toString(); } catch (IOException ex) { Logger.getLogger(TaskEvent.class.getName()).log(Level.SEVERE, null, ex); }finally{ try { dos.close(); bos.close(); } catch (IOException ex) { Logger.getLogger(TaskEvent.class.getName()).log(Level.SEVERE, null, ex); } } ByteArrayInputStream bis=new ByteArrayInputStream(records); DataInputStream dis=new DataInputStream(bis); try { int size=dis.readInt(); for(int i=0;i<size;i++){ //int taskId=dis.readInt(); int taskType=dis.readInt(); String taskName=dis.readUTF(); int count=dis.readInt(); System.out.println("/tType:"+taskType+"/tname:"+taskName+"/tcount:"+count); } } catch (IOException ex) { Logger.getLogger(TaskEvent.class.getName()).log(Level.SEVERE, null, ex); }finally{ try { dis.close(); bis.close(); } catch (IOException ex) { Logger.getLogger(TaskEvent.class.getName()).log(Level.SEVERE, null, ex); } }