import java.util.HashMap;
public class Serialization {
public static void main(String[] args) throws Exception {
Map<String, String> map = new HashMap<>();
byte[] bytes = objectToByte(map);
String toHexString = byteArrayToHexString(bytes);
System.out.println("16进制字符串内容"+toHexString);
byte[] newBytes = hexStringToByteArray(toHexString);
Object obj = byteToObject(newBytes);
HashMap<String, String> newMap = (HashMap<String, String>) obj;
for (Map.Entry<String, String> entry : newMap.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
public static byte[] objectToByte(Object obj) throws Exception {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
byte[] bytes = bo.toByteArray();
public static Object byteToObject(byte[] bytes) throws Exception {
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
ObjectInputStream oi = new ObjectInputStream(bi);
Object obj = oi.readObject();
private static final char[] Chars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static final String HexString = "0123456789abcdef";
public static String byteArrayToHexString(byte[] arr) {
char[] c = new char[arr.length * 2];
c[index++] = Chars[a & 0xf];
c[index++] = Chars[a >>> 4 & 0xf];
String content = new String(c);
public static byte[] hexStringToByteArray(String content) {
byte[] arr = new byte[content.length() / 2];
char[] crr = content.toCharArray();
for (int i = 0; i < crr.length; ) {
int low = HexString.indexOf(crr[i++]);
int high = HexString.indexOf(crr[i++]);
arr[index++] = (byte) (high << 4 | low);