Java 将ArrayList转换为字节数组
我有一段代码,将数组列表转换为字节数组,然后将该字节数组保存为MySQL数据库中的BLOB。代码如下:-Java 将ArrayList转换为字节数组,java,mysql,arraylist,Java,Mysql,Arraylist,我有一段代码,将数组列表转换为字节数组,然后将该字节数组保存为MySQL数据库中的BLOB。代码如下:- Object temp = attributes.get(columnName); if (temp instanceof List && temp != null) { List extraAttributes = (ArrayList) temp; resultStmt.setBytes(currentIndex, createByteArray(extr
Object temp = attributes.get(columnName);
if (temp instanceof List && temp != null) {
List extraAttributes = (ArrayList) temp;
resultStmt.setBytes(currentIndex, createByteArray(extraAttributes));
方法createByteArray
定义如下:
private byte [] createByteArray( Object obj)
{
byte [] bArray = null;
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream objOstream = new ObjectOutputStream(baos);
objOstream.writeObject(obj);
bArray = baos.toByteArray();
}
catch (Exception e)
{
TraceDbLog.writeError("Problem in createByteArray", e);
}
return bArray;
}
上面的代码是在前面编写的,用于将HashMap写入BLOB,我正在使用相同的代码将ArrayList if HashMap转换为BLOB
当我读取blob时,在读取代码时发生的问题
private Object readBytes (ResultSet rs, String columnName)
throws SQLException
{
ObjectInputStream ois = null;
byte [] newArray;
Object obj = null;
try
{
newArray = rs.getBytes(columnName);
ois = new ObjectInputStream (new ByteArrayInputStream(newArray));
obj = ois.readObject ();
}
在读取部分,对象不是作为hasMap的arrayList出现的,在eclipse的调试透视图中,eclipse也无法检查即将出现的对象
我也尝试过输入要列出的对象,但仍然没有得到正确的响应。
请告诉我在读取/写入上述BLOB时是否存在任何缺陷。我已添加了将ArrayList转换为byte[]的示例编码。 一种合理的方法是对列表中的每个字符串使用UTF-8编码,就像DataOutputStream一样。对于字符串,它写入2个字节作为UTF-8编码的长度,后跟UTF-8字节 如果您不在另一端使用Java,那么这将是可移植的。下面是对ArrayList进行编码和解码的示例:
// example input list
List<String> list = new ArrayList<String>();
list.add("foo");
list.add("bar");
list.add("baz");
// write to byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baos);
for (String element : list) {
out.writeUTF(element);
}
byte[] bytes = baos.toByteArray();
// read from byte array
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
DataInputStream in = new DataInputStream(bais);
while (in.available() > 0) {
String element = in.readUTF();
System.out.println(element);
}
//示例输入列表
列表=新的ArrayList();
列表。添加(“foo”);
列表。添加(“酒吧”);
列表。添加(“baz”);
//写入字节数组
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
DataOutputStream out=新的DataOutputStream(BAS);
for(字符串元素:列表){
out.writeUTF(元素);
}
byte[]bytes=baos.toByteArray();
//从字节数组读取
ByteArrayInputStream bais=新的ByteArrayInputStream(字节);
DataInputStream in=新的DataInputStream(BAI);
while(in.available()>0){
String元素=in.readUTF();
系统输出打印项次(元素);
}
最简单的方法是将其转换为json字符串,然后再转换为字节
Gson gson = new Gson();
Type type = new TypeToken<List<Alarm>>() {}.getType();
String json = gson.toJson(list, type);
byte[] bytes = json.getBytes();
Gson-Gson=new-Gson();
Type Type=new-TypeToken(){}.getType();
字符串json=gson.toJson(列表,类型);
byte[]bytes=json.getBytes();
读取对象后,您会得到什么对象?如果您执行System.out.println(obj.getClass().getName())它显示com.sun.jdi.InvocationtargetException,这意味着eclipse无法解析,那么它将打印什么。尽管此异常出现在调试透视图中,但当代码移到catch块中时,程序执行是normalis obj=ois.readObject();执行代码时失败?如果它没有失败,它将返回什么类型的对象在日志中我打印了obj.getClass().getName(),它返回的java.util.ArrayListSo读取对象将按预期返回ArrayList。你面临的确切问题是什么?列表中的数据不是您所期望的吗?为了防止需要使用xml解组器解析内置字节,需要匹配编码方案。在我的例子中,它不在UTF-8中,所以我只需要使用out.writeBytes而不是out.writeUTF。否则,封送员会抛出异常,表示序言中出现意外内容。仅供参考。