• 对象序列化运用


    1. import java.io.*;
    2. import java.util.HashMap;
    3. import java.util.Map;
    4. /**
    5. * 自定义对象序列化
    6. * 将对象与字符串互转
    7. */
    8. public class Serialization {
    9. public static void main(String[] args) throws Exception {
    10. Map<String, String> map = new HashMap<>();
    11. map.put("k1", "v1");
    12. map.put("k2", "v2");
    13. map.put("k3", "v3");
    14. byte[] bytes = objectToByte(map);//转字节
    15. String toHexString = byteArrayToHexString(bytes);//转字符串
    16. System.out.println("16进制字符串内容"+toHexString);
    17. byte[] newBytes = hexStringToByteArray(toHexString);//转字节
    18. Object obj = byteToObject(newBytes);//字节还原对象
    19. HashMap<String, String> newMap = (HashMap<String, String>) obj;//验证
    20. for (Map.Entry<String, String> entry : newMap.entrySet()) {
    21. System.out.println(entry.getKey() + ":" + entry.getValue());
    22. }
    23. }
    24. /**
    25. * 对象转字节
    26. *
    27. * @param obj 对象
    28. * @return 字节信息
    29. * @throws Exception
    30. */
    31. public static byte[] objectToByte(Object obj) throws Exception {
    32. try {
    33. ByteArrayOutputStream bo = new ByteArrayOutputStream();
    34. ObjectOutputStream oo = new ObjectOutputStream(bo);
    35. oo.writeObject(obj);
    36. byte[] bytes = bo.toByteArray();
    37. bo.close();
    38. oo.close();
    39. return (bytes);
    40. } catch (Exception e) {
    41. throw e;
    42. }
    43. }
    44. /**
    45. * 字节还原对象
    46. *
    47. * @param bytes
    48. * @return
    49. * @throws Exception
    50. */
    51. public static Object byteToObject(byte[] bytes) throws Exception {
    52. try {
    53. //bytearray to object
    54. ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    55. ObjectInputStream oi = new ObjectInputStream(bi);
    56. Object obj = oi.readObject();
    57. bi.close();
    58. oi.close();
    59. return obj;
    60. } catch (Exception e) {
    61. throw e;
    62. }
    63. }
    64. private static final char[] Chars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    65. private static final String HexString = "0123456789abcdef";
    66. /**
    67. * 字节数组转换成16进制字符串
    68. *
    69. * @param arr
    70. * @return content
    71. */
    72. public static String byteArrayToHexString(byte[] arr) {
    73. char[] c = new char[arr.length * 2];
    74. int index = 0;
    75. for (byte a : arr) {
    76. c[index++] = Chars[a & 0xf];
    77. c[index++] = Chars[a >>> 4 & 0xf];
    78. }
    79. String content = new String(c);
    80. return content;
    81. }
    82. /**
    83. * 16进制字符串转换成字节数组
    84. *
    85. * @param content
    86. * @return
    87. */
    88. public static byte[] hexStringToByteArray(String content) {
    89. byte[] arr = new byte[content.length() / 2];
    90. char[] crr = content.toCharArray();
    91. int index = 0;
    92. for (int i = 0; i < crr.length; ) {
    93. int low = HexString.indexOf(crr[i++]);
    94. int high = HexString.indexOf(crr[i++]);
    95. arr[index++] = (byte) (high << 4 | low);
    96. }
    97. return arr;
    98. }
    99. }

  • 相关阅读:
    本地搭建Stable-Diffusion 教程
    安卓毕业设计源码基于Uniapp+SSM实现的校园心理健康APP
    android view获取到焦点高亮效果如何去掉
    信息论随笔(二)信息熵及其性质
    动态规划-01背包问题新解(c)
    mydumper 介绍及使用
    国家开放大学 练习题
    SFI立昌Common Mode Filter方案与应用
    【createWrapper】根据条件类创建查询wrapper
    实例演示如何使用CCE XGPU虚拟化
  • 原文地址:https://blog.csdn.net/weixin_42660202/article/details/125419848