Map 转换成byte[] 数组

时间:2022-04-24 07:19:32

把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();
}