Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. ```java
  2. package com.fanglian.modules.test.web;
  3.  
  4. import java.io.Serializable;
  5. import java.util.Date;
  6.  
  7. public class Person implements Serializable {
  8.  
  9. private static final long serialVersionUID = 1L;
  10.  
  11. private String name;
  12. private Integer age;
  13. private static String address;
  14. private transient Date birthday;
  15.  
  16. public Date getBirthday() {
  17. return birthday;
  18. }
  19.  
  20. public void setBirthday(Date birthday) {
  21. this.birthday = birthday;
  22. }
  23.  
  24. public Person(String name, Integer age, Date birthday) {
  25. this.name = name;
  26. this.age = age;
  27. this.birthday = birthday;
  28. }
  29.  
  30. public String getName() {
  31. return name;
  32. }
  33.  
  34. public void setName(String name) {
  35. this.name = name;
  36. }
  37.  
  38. public Integer getAge() {
  39. return age;
  40. }
  41.  
  42. public void setAge(Integer age) {
  43. this.age = age;
  44. }
  45.  
  46. public static String getAddress() {
  47. return address;
  48. }
  49.  
  50. public static void setAddress(String address) {
  51. Person.address = address;
  52. }
  53.  
  54. @Override
  55. public String toString() {
  56. return "Person{" +
  57. "name='" + name + '\'' +
  58. ", age=" + age +
  59. '}';
  60. }
  61. }
  62.  
  63. package com.fanglian.modules.test.web;
  64.  
  65. import java.io.*;
  66. import java.util.Date;
  67.  
  68. /**
  69. * java序列化与反序列化:
  70. * 序列化:字节序列转换为对象的过程
  71. * 反序列化:对象转换为字节序列的过程
  72. * 被transient修饰的属性是不会被序列化的
  73. * 静态属性也是不能被序列化,和反序列化
  74. * 可序列化的对象,如果没有指定serialVersionUID,java默认会生产一个serialVersionUID,但是序列化和反序列化生成
  75. * 的serialVersionUID是不同的,所以推荐手动指定serialVersionUID值,如:
  76. * private final static long serialVersionUID = 1L;
  77. * 所以实现Serializable接口的时候,一定要给这个serialVersionUID赋值!
  78. * 对象序列化机制:是java语言内建的一种持久化方式,通过对象序列化,可以把对象状态保持为字节数组,并且可以在需要的时候
  79. * 将字节数组反序列化的方式转换成为对象,对象序列化可以很容易的在JVM中活动对象和字节数组之家转换
  80. * 在java中对象序列化和反序列化被广泛应用到RMI(远程方法调用)以及网络传输中
  81. */
  82. public class Test {
  83.  
  84. public static void main(String[] args)throws Exception {
  85. m1();
  86. Person person = m2();
  87. System.out.println(person.toString());
  88. }
  89.  
  90. //序列化
  91. public static void m1()throws Exception {
  92. Person person = new Person("jack", 22, new Date());
  93. String path = "C:/Users/Administrator/Desktop/person.txt";
  94. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(path)));
  95. oos.writeObject(person);
  96. oos.flush();
  97. oos.close();
  98. System.out.println("对象序列化完成!");
  99. }
  100.  
  101. //反序列化
  102. public static Person m2()throws Exception {
  103. String path = "C:/Users/Administrator/Desktop/person.txt";
  104. ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(path)));
  105. Person o = (Person) ois.readObject();
  106. ois.close();
  107. System.out.println("对象反序列化完成!");
  108. return o;
  109. }
  110. }
  111. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement