在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java 字符串系列化及反系列化

2016-12-20 13:17| 发布者: zhangjf| 查看: 500| 评论: 0

摘要: /** * 字符串序列化 * @param str * @return */ public static String compareSerialization(String str) { String result = ; try { ByteArrayOutputStream byteArray ...
  1. /**
  2. * 字符串序列化
  3. * @param str
  4. * @return
  5. */
  6. public static String compareSerialization(String str) {
  7. String result = "";
  8. try {
  9. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  10. ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
  11. objectOutputStream.writeObject(str);
  12. result = byteArrayOutputStream.toString("ISO-8859-1");
  13. result = java.net.URLEncoder.encode(result, "UTF-8");
  14. objectOutputStream.close();
  15. byteArrayOutputStream.close();
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. return result;
  20. }
  21. /**
  22. * 字符串反序列化
  23. * @param serStr
  24. * @return
  25. */
  26. public static String deSerialization(String serStr) {
  27. String result = "";
  28. try {
  29. result = java.net.URLDecoder.decode(serStr, "UTF-8");
  30. ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(result.getBytes("ISO-8859-1"));
  31. ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
  32. result = (String) objectInputStream.readObject();
  33. objectInputStream.close();
  34. byteArrayInputStream.close();
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. } finally {
  38. return result;
  39. }
  40. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-8-23 04:25

Copyright 2015-2025 djqfx

返回顶部