Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5887
  • 博文数量: 1
  • 博客积分: 22
  • 博客等级: 民兵
  • 技术积分: 20
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-23 19:20
文章分类

全部博文(1)

文章存档

2016年(1)

我的朋友
最近访客

分类: Java

2016-08-14 12:14:08

最近想把一些Java对象 保存进数据库的某个字段里,到需要的时候一读出来就直接可以用了。
查找了一下网上的资料,都是关于Object.toString()的,于是自己动手丰衣足食。用ObjectOutputStream 和 ObjectInputStream.

点击(此处)折叠或打开

  1. import java.io.ObjectInputStream;
  2. import java.io.ObjectOutputStream;
  3. import java.io.PipedInputStream;
  4. import java.io.PipedOutputStream;
  5. import java.io.Serializable;

  6. class myObj implements Serializable{
  7.     public String name;
  8.     public int amount;
  9.     
  10.     public myObj(String n, int a){
  11.         name = n;
  12.         amount =a;
  13.     }
  14.     
  15.     public String toString(){
  16.         return name + ": " + amount;
  17.     }
  18. }
  19. public class Test{
  20.     
  21.     public static String objectToString(Object o) throws Exception {
  22.         
  23.         PipedInputStream pis = new PipedInputStream();
  24.         PipedOutputStream pos = new PipedOutputStream(pis);

  25.         ObjectOutputStream oos = new ObjectOutputStream(pos);
  26.         oos.writeObject(o);
  27.         oos.close();
  28.         
  29.         int count = pis.available();
  30.         byte[] buf = new byte[count];
  31.         pis.read(buf);
  32.         pis.close();
  33.         pos.close();

  34.         return Base64.getBase64(buf);
  35.     }
  36.     
  37.     public static Object stringToObject(String s) throws Exception {
  38.         byte[] buf = Base64.base64ToByte(s);
  39.         
  40.         PipedInputStream pis = new PipedInputStream();
  41.         PipedOutputStream pos = new PipedOutputStream(pis);
  42.         pos.write(buf);
  43.         
  44.         ObjectInputStream ois = new ObjectInputStream(pis);
  45.         Object o = ois.readObject();
  46.         ois.close();
  47.         pis.close();
  48.         pos.close();

  49.         return o;
  50.     }
  51.     
  52.     public static void main(String[] args) throws Exception {
  53. //        Stream s = Files.lines(Paths.get("C:\\Users\\Ems\\Documents\\莲师消除障道祈请颂.txt"));
  54. //        s.filter(line -> !line.trim().equals(""))
  55. //        .forEach(line -> System.out.println(line));
  56. //        s.close();
  57. //        PipedInputStream pis = new PipedInputStream();
  58. //        PipedOutputStream pos = new PipedOutputStream(pis);
  59. //
  60. //        ObjectOutputStream oos = new ObjectOutputStream(pos);
  61. //        oos.writeObject(new myObj("Emerson", 1000000));
  62. //        
  63. //        ObjectInputStream ois = new ObjectInputStream(pis);
  64. //        myObj mo = (myObj) ois.readObject();
  65.         
  66.         String hey = objectToString(new myObj("Emerson", 1000000));
  67.         System.out.println(hey);
  68.         
  69.         myObj mo = (myObj) stringToObject(hey);
  70.         
  71.         System.out.println(mo.toString());
  72.     }
  73.     
  74. }

运行结果:

点击(此处)折叠或打开

  1. rO0ABXNyAAVteU9iaunFwI4Djwo8AgACSQAGYW1vdW50TAAEbmFtZXQAEkxqYXZhL2xhbmcvU3Ry
  2. aW5nO3hwAA9CQHQAB0VtZXJzb24=
  3. Emerson: 1000000


附上 Base64.java

点击(此处)折叠或打开

  1. import java.io.UnsupportedEncodingException;

  2. import sun.misc.*;

  3. public class Base64 {
  4.     // 加密
  5.     public static String getBase64(String str) {
  6.         byte[] b = null;
  7.         String s = null;
  8.         try {
  9.             b = str.getBytes("utf-8");
  10.         } catch (UnsupportedEncodingException e) {
  11.             e.printStackTrace();
  12.         }
  13.         if (b != null) {
  14.             s = new BASE64Encoder().encode(b);
  15.         }
  16.         return s;
  17.     }

  18.     public static String getBase64(byte[] b) {
  19.         String s = null;

  20.         if (b != null) {
  21.             s = new BASE64Encoder().encode(b);
  22.         }
  23.         return s;
  24.     }

  25.     // 解密
  26.     public static byte[] base64ToByte(String s) {
  27.         byte[] b = null;
  28.         if (s != null) {
  29.             BASE64Decoder decoder = new BASE64Decoder();
  30.             try {
  31.                 b = decoder.decodeBuffer(s);
  32.             } catch (Exception e) {
  33.                 e.printStackTrace();
  34.             }
  35.         }
  36.         return b;
  37.     }

  38.     // 解密
  39.     public static String getFromBase64(String s) {
  40.         byte[] b = null;
  41.         String result = null;
  42.         if (s != null) {
  43.             BASE64Decoder decoder = new BASE64Decoder();
  44.             try {
  45.                 b = decoder.decodeBuffer(s);
  46.                 result = new String(b, "utf-8");
  47.             } catch (Exception e) {
  48.                 e.printStackTrace();
  49.             }
  50.         }
  51.         return result;
  52.     }
  53. }

阅读(2231) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~