您的位置:首页 > Web前端 > JavaScript

字节数组 与对象流操作

2009-09-02 14:40 169 查看
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();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息