[英]Convert byte[] to ArrayList<String>
I found a question here on SO: Convert ArrayList<String> to byte [] 我在SO上找到了一个问题: 将ArrayList <String>转换为byte []
It is about converting ArrayList<String>
to byte[]
. 它是关于将ArrayList<String>
转换为byte[]
。
Now is it possible to convert byte[]
to ArrayList<String>
? 现在可以将byte[]
转换为ArrayList<String>
吗?
Looks like nobody read the original question :) 看起来没有人读原来的问题:)
If you used the method from the first answer to serialize each string separately, doing exactly the opposite will yield the required result: 如果您使用第一个答案中的方法分别序列化每个字符串,则完全相反的操作将产生所需的结果:
ByteArrayInputStream bais = new ByteArrayInputStream(byte[] yourData);
ObjectInputStream ois = new ObjectInputStream(bais);
ArrayList<String> al = new ArrayList<String>();
try {
Object obj = null;
while ((obj = ois.readObject()) != null) {
al.add((String) obj);
}
} catch (EOFException ex) { //This exception will be caught when EOF is reached
System.out.println("End of file reached.");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
//Close the ObjectInputStream
try {
if (ois != null) {
ois.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
If your byte[] contains the ArrayList itself, you can do: 如果你的byte []包含ArrayList本身,你可以这样做:
ByteArrayInputStream bais = new ByteArrayInputStream(byte[] yourData);
ObjectInputStream ois = new ObjectInputStream(bais);
try {
ArrayList<String> arrayList = ( ArrayList<String>) ois.readObject();
ois.close();
} catch (EOFException ex) { //This exception will be caught when EOF is reached
System.out.println("End of file reached.");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
//Close the ObjectInputStream
try {
if (ois!= null) {
ois.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Something like this should suffice, forgive any compile typos I've just rattled it out here: 像这样的东西应该足够了,原谅任何编译错字我只是在这里喋喋不休:
for(int i = 0; i < allbytes.length; i++)
{
String str = new String(allbytes[i]);
myarraylist.add(str);
}
yeah its possible, take each item from byte array and convert to string, then add to arraylist 是的可能,从字节数组中取出每个项目并转换为字符串,然后添加到arraylist
String str = new String(byte[i]);
arraylist.add(str);
it depends very much on the semantics you expect from such a method. 它很大程度上取决于你对这种方法的期望。 The easiest way would be, new String(bytes, "US-ASCII")
—and then split it into the details you want. 最简单的方法是使用new String(bytes, "US-ASCII")
- 然后将其拆分为您想要的详细信息。
There are obviously some problems: 显然有一些问题:
"US-ASCII"
and not "UTF8"
or, say, "Cp1251"
? 我们怎样才能确定它是"US-ASCII"
而不是"UTF8"
或者说"Cp1251"
? And so on and so forth. 等等等等。 But the easiest way is indeed to call String
constructor—it'll be enough to get you started. 但最简单的方法是调用String
构造函数 - 这足以让你开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.