Java:对象到字节 [] 和字节 [] 到对象转换器(用于东京内阁) [英] Java: object to byte[] and byte[] to object converter (for Tokyo Cabinet)
本文介绍了Java:对象到字节 [] 和字节 [] 到对象转换器(用于东京内阁)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将对象转换为 byte[] 以存储在 Tokyo Cabinet 键值存储中.从键值存储读取时,我还需要将 byte[] 取消字节到对象.
I need to convert objects to a byte[] to be stored in the Tokyo Cabinet key-value store. I also need to unbyte the byte[] to an Object when reading from the key-value store.
是否有任何软件包可以帮助我完成这项任务?或者自己实施它的最佳解决方案是什么?
Are there any packages out there that will help me with this task? Or would the best solution to implement it myself?
推荐答案
public static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(obj);
return out.toByteArray();
}
public static Object deserialize(byte[] data) throws IOException, ClassNotFoundException {
ByteArrayInputStream in = new ByteArrayInputStream(data);
ObjectInputStream is = new ObjectInputStream(in);
return is.readObject();
}
这篇关于Java:对象到字节 [] 和字节 [] 到对象转换器(用于东京内阁)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文