Java序列化对象为字符串并将字符串反序列化为对象
Posted 卫旗
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java序列化对象为字符串并将字符串反序列化为对象相关的知识,希望对你有一定的参考价值。
对象的序列化与反序列化其实就是将对象的状态保存下来,一般是保存到文件中,但是其实更常用的是将对象序列化为字符串保存到数据库中,然后在需要读取对象的情况下将字符串反序列化为对象。
可以序列化的类必须实现Serializabe接口,此处需要注意的是如果某个属性不需要被序列化,可以添加transient关键字。
下面是一个可以被序列化的类:
package demo1; import java.io.Serializable; public class Person implements Serializable{ private static final long serialVersionUID = 1L; private int id; private String name; private int age; private transient String sex; public Person(int id, String name, int age,String sex) { super(); this.id = id; this.name = name; this.age = age; this.sex = sex; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return this.id+" "+this.name+" "+this.age+" "+this.sex; } }
下面是封装序列化的类:
package demo1; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializeUtils { public static String serialize(Object obj) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream; objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(obj); String string = byteArrayOutputStream.toString("ISO-8859-1"); objectOutputStream.close(); byteArrayOutputStream.close(); return string; } public static Object serializeToObject(String str) throws IOException, ClassNotFoundException { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(str.getBytes("ISO-8859-1")); ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); Object object = objectInputStream.readObject(); objectInputStream.close(); byteArrayInputStream.close(); return object; } }
下面是使用redis简单的将对象序列化后保存到数据库中然后再从数据库中取出数据反序列化的过程:
public class DemoAtomic { public static void main(String [] args) throws IOException, ClassNotFoundException { Jedis jedis = new Jedis("localhost",6379); jedis.setex("test1", 60*60*24,SerializeUtils.serialize(new Person(1, "测试1", 21,"男"))); jedis.setex("test2", 60*60*24,SerializeUtils.serialize(new Person(2, "测试2", 21,"女"))); jedis.setex("test3", 60*60*24,SerializeUtils.serialize(new Person(3, "测试3", 21,"男"))); jedis.setex("test4", 60*60*24,SerializeUtils.serialize(new Person(4, "测试4", 21,"女"))); Set<String> keys = jedis.keys("*"); for (String s:keys) { System.out.println(s); Person person = (Person)SerializeUtils.serializeToObject(jedis.get(s)); System.out.println(person.toString()); } } }
下面为打印数据:
test4 4 测试4 21 null test2 2 测试2 21 null test3 3 测试3 21 null test1 1 测试1 21 null
以上是关于Java序列化对象为字符串并将字符串反序列化为对象的主要内容,如果未能解决你的问题,请参考以下文章