1. 程式人生 > >Map 轉換成byte[] 數組

Map 轉換成byte[] 數組

you name try obj oos exceptio () trace trac

把Map轉換成byte數組,使用 ByteArrayOutputStream和ObjectOutputStream

Map<String,String> map = new HashMap<String,String>();
map.put("name","aaa");
map.put("age","11");
try {
byte[] bt = null;
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(map);
bt=os.toByteArray();

oos.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Map 轉換成byte[] 數組