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

来源:互联网 发布:java jsf框架 编辑:程序博客网 时间:2024/06/02 06:07

将序列化的对象转成字节数据,将字节数据存放到数据库;

从数据库取出来是以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;
}
}
1 0
原创粉丝点击