Java 将 ArrayList 转换为字节数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20869325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert ArrayList to byte array
提问by Timothy Drisdelle
I have a code where I am converting array list to byte array and then saving that byte array as a BLOB in MySQL database. Below is code:-
我有一个代码,我将数组列表转换为字节数组,然后将该字节数组保存为 MySQL 数据库中的 BLOB。以下是代码:-
Object temp = attributes.get(columnName);
if (temp instanceof List && temp != null) {
List extraAttributes = (ArrayList) temp;
resultStmt.setBytes(currentIndex, createByteArray(extraAttributes));
The method createByteArray
is defined as below:
该方法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;
}
Well the above code was written earlier for writing HashMap to BLOB i am using same for converting ArrayList if HashMap to BLOB.
好吧,上面的代码是之前编写的,用于将 HashMap 写入 BLOB,如果将 HashMap 转换为 BLOB,我正在使用相同的代码将 ArrayList 转换。
The problem which is occurring in read code when i am reading the 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 ();
}
In the read part the object is not coming as arrayList of hasMap and in debug perspective in eclipse eclipse is also not able to inspect the object which is coming.
在读取部分,对象不是作为 hasMap 的 arrayList 出现的,并且在 eclipse 的调试透视图中,eclipse 也无法检查即将到来的对象。
I have also tried typecasting the object to List but still no success in getting the right response. Please tell me whether there is any flaw in reading/writing the above BLOB.
我也尝试将对象类型转换为 List 但仍然没有成功获得正确的响应。请告诉我读/写上述BLOB是否有任何缺陷。
回答by Kannan Arumugam
i have added sample coding for convert ArrayList to byte[]. One reasonable way would be to use UTF-8 encoding like DataOutputStream does for each string in the list. For a string it writes 2 bytes for the length of the UTF-8 encoding followed by the UTF-8 bytes. This would be portable if you're not using Java on the other end. Here's an example of encoding and decoding an ArrayList:
我添加了用于将 ArrayList 转换为 byte[] 的示例编码。一种合理的方法是使用 UTF-8 编码,就像 DataOutputStream 对列表中的每个字符串所做的那样。对于字符串,它为 UTF-8 编码的长度写入 2 个字节,然后是 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);
}
回答by Ananthu Subramanian
The easiest way is to convert it to json string and then to bytes
最简单的方法是将其转换为json字符串,然后再转换为字节
Gson gson = new Gson();
Type type = new TypeToken<List<Alarm>>() {}.getType();
String json = gson.toJson(list, type);
byte[] bytes = json.getBytes();