java io流

本文详细介绍并演示了Java中字节数组与字节数组流、对象流的操作过程,包括序列化与反序列化的具体实现。同时,还探讨了如何通过脚本引擎执行简单的脚本代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//---JAVA字节数组与字节数组流操作---------------------------------------------------
  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);
            }

        }

     
//---JAVA字节数组与对象流操作---------------------------------------------------        
byte record[] = null;
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        ObjectOutputStream oos=null;
        try{
            oos=new ObjectOutputStream(bos);
            Map map=new HashMap();


            Map m1=new HashMap();
            m1.put("type", Task.KILL_TYPE);
            m1.put("name", "1_兔子");
            m1.put("npc", "兔子");
            m1.put("count", 1);

            map.put(Task.KILL_TYPE+"1_兔子", m1);
            oos.writeObject(map);
            record=bos.toByteArray();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                oos.close();
                bos.close();
            } catch (IOException ex) {
                Logger.getLogger(RoleEventHandle.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        ByteArrayInputStream bis=new ByteArrayInputStream(record);
        ObjectInputStream ois=null;
        try{
            ois=new ObjectInputStream(bis);
            Map myMap=(Map) ois.readObject();
            Map myMap1=(Map) myMap.get(Task.KILL_TYPE+"1_兔子");
            String npc=(String) myMap1.get("npc");
            System.out.println(npc);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                ois.close();
                bis.close();
            } catch (IOException ex) {
                Logger.getLogger(RoleEventHandle.class.getName()).log(Level.SEVERE, null, ex);
            }
        }




 
//---JAVA执行脚本--------------------------------------------------------

       ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
       Boolean bool=false;
        try{

        Compilable compilable = (Compilable) engine;  编译器
        CompiledScript compiled = compilable.compile("");
         bool=(Boolean) compiled.eval();
         System.out.println(bool);
        }catch(Exception e){
            e.printStackTrace();

        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值