|
| 1 | +package cn.aofeng.demo.java.lang.serialization; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.ObjectInputStream; |
| 8 | +import java.io.ObjectOutputStream; |
| 9 | + |
| 10 | +import org.apache.commons.io.IOUtils; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | + |
| 14 | +/** |
| 15 | + * 关键字 transient 测试。 |
| 16 | + * |
| 17 | + * @author <a href="mailto:[email protected]">聂勇</a> |
| 18 | + */ |
| 19 | +public class TransientDemo { |
| 20 | + |
| 21 | + private static Logger _logger = LoggerFactory.getLogger(TransientDemo.class); |
| 22 | + |
| 23 | + private String _tempFileName = "TransientDemo"; |
| 24 | + |
| 25 | + /** |
| 26 | + * 将对象序列化并保存到文件。 |
| 27 | + * |
| 28 | + * @param obj 待序列化的对象 |
| 29 | + */ |
| 30 | + public void save(Object obj) { |
| 31 | + ObjectOutputStream outs = null; |
| 32 | + try { |
| 33 | + outs = new ObjectOutputStream( |
| 34 | + new FileOutputStream( getTempFile(_tempFileName) )); |
| 35 | + outs.writeObject(obj); |
| 36 | + } catch (IOException e) { |
| 37 | + _logger.error("save object to file occurs error", e); |
| 38 | + } finally { |
| 39 | + IOUtils.closeQuietly(outs); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * 从文件读取内容并反序列化成对象。 |
| 45 | + * |
| 46 | + * @return {@link People}对象。如果读取文件出错 或 对象类型转换失败,返回null。 |
| 47 | + */ |
| 48 | + public <T> T load() { |
| 49 | + ObjectInputStream ins = null; |
| 50 | + try { |
| 51 | + ins = new ObjectInputStream( |
| 52 | + new FileInputStream( getTempFile(_tempFileName)) ); |
| 53 | + return ((T) ins.readObject()); |
| 54 | + } catch (IOException e) { |
| 55 | + _logger.error("load object from file occurs error", e); |
| 56 | + } catch (ClassNotFoundException e) { |
| 57 | + _logger.error("load object from file occurs error", e); |
| 58 | + } finally { |
| 59 | + IOUtils.closeQuietly(ins); |
| 60 | + } |
| 61 | + |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + private File getTempFile(String filename) { |
| 66 | + return new File(getTempDir(), filename); |
| 67 | + } |
| 68 | + |
| 69 | + private String getTempDir() { |
| 70 | + return System.getProperty("java.io.tmpdir"); |
| 71 | + } |
| 72 | + |
| 73 | + private void displayPeople(People people) { |
| 74 | + if (null == people) { |
| 75 | + return; |
| 76 | + } |
| 77 | + String template = "People[name:%s, age:%d, address:%s, sTestNormal:%s, sTestTransient:%s]"; |
| 78 | + System.out.println( String.format(template, people.getName(), people.getAge(), |
| 79 | + people.getAddress(), people.getsTestNormal(), people.getsTestTransient())); |
| 80 | + } |
| 81 | + |
| 82 | + private void displayMan(Man man) { |
| 83 | + if (null == man) { |
| 84 | + return; |
| 85 | + } |
| 86 | + String template = "Man[manName:%s, manAge:%d, password:%s]"; |
| 87 | + System.out.println( String.format(template, man.getManName(), man.getManAge(), man.getPassword()) ); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param args |
| 92 | + */ |
| 93 | + public static void main(String[] args) { |
| 94 | + System.out.println(">>> Serializable测试"); |
| 95 | + TransientDemo demo = new TransientDemo(); |
| 96 | + |
| 97 | + People people = new People("张三", 30, "中国广州"); |
| 98 | + people.setsTestNormal("normal-first"); |
| 99 | + people.setsTestTransient("transient-first"); |
| 100 | + System.out.println("序列化之前的对象信息:"); |
| 101 | + demo.displayPeople(people); |
| 102 | + demo.save(people); |
| 103 | + |
| 104 | + // 修改静态变量的值 |
| 105 | + people.setsTestNormal("normal-second"); |
| 106 | + people.setsTestTransient("transient-second"); |
| 107 | + |
| 108 | + People fromLoad = demo.load(); |
| 109 | + System.out.println("反序列化之后的对象信息:"); |
| 110 | + demo.displayPeople(fromLoad); |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + System.out.println(""); |
| 115 | + System.out.println(">>> Externalizable测试"); |
| 116 | + Man man = new Man("李四", 10, "假密码"); |
| 117 | + System.out.println("序列化之前的对象信息:"); |
| 118 | + demo.displayMan(man); |
| 119 | + demo.save(man); |
| 120 | + |
| 121 | + Man manFromLoadMan = demo.load(); |
| 122 | + System.out.println("反序列化之后的对象信息:"); |
| 123 | + demo.displayMan(manFromLoadMan); |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments