您的位置:首页 > 数据库

将一个序列化的对象存放到数据库的方法

2016-01-26 09:49 351 查看
将序列化的对象转成字节数据,将字节数据存放到数据库;

从数据库取出来是以getBlod的方法得到字节数据

public class TypeChangeTool {

private static final String TAG = TypeChangeTool.class.getSimpleName();
public static byte[] toByte(Object object) {
Log.d(TAG, "TypeChangeTool toByte()");
byte[] data = null;
if(object != null){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(object);
objectOutputStream.flush();
data = byteArrayOutputStream.toByteArray();
Log.d(TAG, "TypeChangeTool toByte() success");
} catch (IOException e) {
Log.d(TAG, "TypeChangeTool toByte() throw exception="+e.toString());
e.printStackTrace();
}
}
return data;
}

public static Object toObject(byte[] data) {
Log.d(TAG, "TypeChangeTool toUserTags()");
Object object = null;
if(data != null && data.length > 0){
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
try {
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
object = (Object)objectInputStream.readObject();
byteArrayInputStream.close();
objectInputStream.close();
Log.d(TAG, "TypeChangeTool toUserTags() success");
} catch (Exception e) {
Log.d(TAG, "TypeChangeTool toUserTags() throw exception="+e.toString());
e.printStackTrace();
}
}

return object;
}

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