</pre><pre name="code" class="java"> private static void testString () {
Kryo kryo=new Kryo();
String w_str1="简体中文,繁體中文,English";
//把w_str1对象序列化
Output output=new Output(1024);
kryo.writeObject(output, w_str1);
output.flush();
output.close();
byte[] w_ret= output.toBytes(); //获得byte数据,这些数据可用作储存、网络传输等...
//还原
Input input=new Input(w_ret);
input.close();
String w_str2=kryo.readObject(input, String.class);
System.out.println(w_str2);
}
public static void testHashMap() throws NoSuchAlgorithmException{
Kryo kryo=new Kryo();
HashMap h=new HashMap();
h.put("k1", "v1");
h.put("k2", "v2");
Output output=new Output(1, 1024);
kryo.writeObject(output, h);
output.close();
byte[] data=output.toBytes();
Input i=new Input(data);
i.close();
HashMap h2= (HashMap)kryo.readObject(i, HashMap.class);
System.out.println(h2.get("k2"));
}
public static class TestBean implements Serializable{
private int[] intArray;
private HashMap<String,String> hashMapVal;
private String strVal;
public int[] getIntArray () {
return intArray;
}
public void setIntArray (int[] intArray) {
this.intArray = intArray;
}
public HashMap<String, String> getHashMapVal () {
return hashMapVal;
}
public void setHashMapVal (HashMap<String, String> hashMapVal) {
this.hashMapVal = hashMapVal;
}
public String getStrVal () {
return strVal;
}
public void setStrVal (String strVal) {
this.strVal = strVal;
}
}
public static void testBean() throws NoSuchAlgorithmException{
Kryo kryo=new Kryo();
kryo.register(TestBean.class,new BeanSerializer(kryo, TestBean.class));
TestBean tb1=new TestBean();
tb1.setStrVal("test1");
tb1.setHashMapVal(new HashMap<String,String>());
tb1.getHashMapVal().put("k1", "v1");
tb1.getHashMapVal().put("k2", "v2");
int[] ints=new int[3];
ints[0]=1;
ints[1]=2;
ints[2]=3;
tb1.setIntArray(ints);
Output output=new Output(1, 1024);
kryo.writeObject(output, tb1);
output.close();
byte[] data=output.toBytes();
Input i=new Input(data);
i.close();
TestBean tb2= (TestBean)kryo.readObject(i, TestBean.class);
System.out.println(tb2.strVal);
System.out.println(tb2.hashMapVal.get("k1"));
System.out.println(tb2.intArray[2]);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有