标签:
学习Redis添加Object时,由于Redis只能存取字符串String,对于其它数据类型形容:Int,long,double,Date等不提供支持,因而需要设计到对象的序列化和反序列化.java序列化的过程就是将对象转变为byte,字节码的过程. Java的反序列过程就是就是将字节码恢复成对象的过程。
一正一逆,也迎合了电脑存储数据的特点。
关于Java的序列化和反序列化的Demo:
1 package hbut.base; 2 3 import org.junit.Test; 4 5 import java.io.*; 6 7 /** 8 * @Author XiJun.Gong 9 * @DATE 2016/6/12. 10 * aim: hbut.base 11 * function: 对象的序列化和反序列 12 */ 13 public class serializableDemo { 14 15 private User user = null; 16 private ByteArrayOutputStream byteArrayOutputStream = null; 17 private ObjectOutputStream outputStream = null; 18 private ObjectInputStream inputStream = null; 19 private ByteArrayInputStream byteArrayInputStream = null; 20 private byte[] result; 21 22 23 24 @Test 25 public void test() { 26 user = new User(); 27 user.setUsername("huifeidmeng"); 28 user.setAge(123); 29 30 try { 31 byteArrayOutputStream = new ByteArrayOutputStream(); 32 outputStream = new ObjectOutputStream(byteArrayOutputStream); 33 outputStream.writeObject(user); 34 result = byteArrayOutputStream.toByteArray(); 35 } catch (IOException e1) { 36 e1.printStackTrace(); 37 } finally { 38 try { 39 if (outputStream != null) outputStream.close(); 40 if (byteArrayOutputStream != null) byteArrayOutputStream.close(); 41 } catch (IOException e2) { 42 e2.printStackTrace(); 43 } 44 } 45 /*tmp sout*/ 46 System.out.println(result); 47 User tmpUser = new User(); 48 try { 49 byteArrayInputStream = new ByteArrayInputStream(result); 50 inputStream = new ObjectInputStream(byteArrayInputStream); 51 tmpUser = (User) inputStream.readObject(); 52 } catch (IOException e) { 53 e.printStackTrace(); 54 } catch (ClassNotFoundException e3) { 55 e3.printStackTrace(); 56 } finally { 57 try { 58 if (byteArrayInputStream != null) byteArrayInputStream.close(); 59 if (inputStream != null) inputStream.close(); 60 } catch (IOException e4) { 61 e4.printStackTrace(); 62 } 63 64 } 65 System.out.println(tmpUser.getUsername() + ": " + tmpUser.getAge()); 66 } 67 68 } 69 70 71 class User implements Serializable { 72 private static final long serialVersionUID = -1941046831377985500L; 73 String username; 74 Integer age; 75 76 public User() { 77 } 78 79 public String getUsername() { 80 return username; 81 } 82 83 public void setUsername(String username) { 84 this.username = username; 85 } 86 87 public Integer getAge() { 88 return age; 89 } 90 91 public void setAge(Integer age) { 92 this.age = age; 93 } 94 }
在编写demo的过程中,发现一个问题,当使用内部类作为对象的的时候,序列化总是抛出异常,但是一旦将User类迁出的时候,又恢复正常。
关于序列化和反序列工具类Dmeo:
1 package com.hbut.util; 2 3 import java.io.*; 4 5 /** 6 * @Author XiJun.Gong 7 * @DATE 2016/6/13. 8 * aim: com.hbut.util 9 */ 10 public class SerializableTool { 11 12 private ByteArrayOutputStream byteArrayOutputStream = null; 13 private ObjectOutputStream outputStream = null; 14 private ObjectInputStream inputStream = null; 15 private ByteArrayInputStream byteArrayInputStream = null; 16 17 public byte[] serializable(Object object) { 18 byte[] result = null; 19 try { 20 byteArrayOutputStream = new ByteArrayOutputStream(); 21 outputStream = new ObjectOutputStream(byteArrayOutputStream); 22 outputStream.writeObject(object); 23 result = byteArrayOutputStream.toByteArray(); 24 } catch (IOException e1) { 25 e1.printStackTrace(); 26 } finally { 27 try { 28 if (outputStream != null) outputStream.close(); 29 if (byteArrayOutputStream != null) byteArrayOutputStream.close(); 30 } catch (IOException e2) { 31 e2.printStackTrace(); 32 } 33 } 34 return result; 35 } 36 37 public Object unSerializable(byte[] bytes) { 38 Object object = null; 39 try { 40 byteArrayInputStream = new ByteArrayInputStream(bytes); 41 inputStream = new ObjectInputStream(byteArrayInputStream); 42 object = inputStream.readObject(); 43 } catch (IOException e) { 44 e.printStackTrace(); 45 } catch (ClassNotFoundException e3) { 46 e3.printStackTrace(); 47 } finally { 48 try { 49 if (byteArrayInputStream != null) byteArrayInputStream.close(); 50 if (inputStream != null) inputStream.close(); 51 } catch (IOException e4) { 52 e4.printStackTrace(); 53 } 54 } 55 return object; 56 } 57 }
Redis存取对象和读取对象Demo
1 class UserModel implements Serializable { 2 3 private String username; 4 private String password; 5 private Integer age; 6 7 @Override 8 public String toString() { 9 return "UserModel{" + 10 "username=‘" + username + ‘\‘‘ + 11 ", password=‘" + password + ‘\‘‘ + 12 ", age=" + age + 13 ‘}‘; 14 } 15 16 public UserModel() { 17 18 } 19 20 public Integer getAge() { 21 return age; 22 } 23 24 public void setAge(Integer age) { 25 this.age = age; 26 } 27 28 public String getUsername() { 29 30 return username; 31 } 32 33 public void setUsername(String username) { 34 this.username = username; 35 } 36 37 public String getPassword() { 38 return password; 39 } 40 41 public void setPassword(String password) { 42 this.password = password; 43 } 44 }
该是这部分的继续:
启动Redis服务器,后运行如下代码:
1 package com.hbut.util; 2 3 import com.google.common.collect.Maps; 4 import org.junit.Before; 5 import org.junit.Test; 6 import redis.clients.jedis.Jedis; 7 import redis.clients.jedis.JedisPool; 8 import redis.clients.jedis.JedisPoolConfig; 9 10 import java.io.Serializable; 11 import java.util.Iterator; 12 import java.util.List; 13 import java.util.Map; 14 15 /** 16 * Created by XiJun.Gong on 14-2-28. 17 */ 18 public class TestRedis { 19 20 21 JedisPool pool; 22 Jedis jedis; 23 24 /** 25 * connection 26 */ 27 @Before 28 public void init() { 29 pool = new JedisPool(new JedisPoolConfig(), "localhost"); 30 jedis = pool.getResource(); 31 //jedis.auth("*******"); //密码验证 32 } 33 34 /** 35 * for object* 36 */ 37 @Test 38 public void testObject() { 39 40 SerializableTool serializableTool = new SerializableTool(); 41 UserModel userModel = new UserModel(); 42 userModel.setAge(123); 43 userModel.setUsername("huifeidmeng"); 44 userModel.setPassword("******"); 45 byte[] bytes = serializableTool.serializable(userModel); 46 jedis.set("useModel".getBytes(), bytes); 47 byte[] result = jedis.get("useModel".getBytes()); 48 Object object = serializableTool.unSerializable(result); 49 UserModel tmpUser = (UserModel) object; 50 System.out.println(tmpUser.toString()); 51 } 52 53 }
运行结果:
UserModel{username=‘huifeidmeng‘, password=‘******‘, age=123}
标签:
原文地址:http://www.cnblogs.com/gongxijun/p/5579266.html