PageRenderTime 153ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/java-practice/src/main/java/com/github/jdk8/ebook/java8_recipes2nd_edition/Chapter8Code.java

https://gitlab.com/doctorwho1986/doctor
Java | 119 lines | 78 code | 23 blank | 18 comment | 1 complexity | 19eb774b0338459bb1683831309f66fd MD5 | raw file
  1. package com.github.jdk8.ebook.java8_recipes2nd_edition;
  2. import java.io.Externalizable;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInput;
  8. import java.io.ObjectInputStream;
  9. import java.io.ObjectOutput;
  10. import java.io.ObjectOutputStream;
  11. import com.alibaba.fastjson.JSON;
  12. /**
  13. * One reason you may choose to implement the Externalizable interface instead
  14. * of the Serializable interface is because Java’s default serialization is very
  15. * inefficient. Because the Java Serialization framework needs to ensure that
  16. * every object (and dependent object) is serialized, it will write even objects
  17. * that have default values or that might be empty and/or null. Implementing the
  18. * Externalizable interface also provides for finer-grained control on how your
  19. * class is being serialized.
  20. *
  21. * @author doctor
  22. *
  23. * @since 2015年2月4日 下午10:02:55
  24. */
  25. public class Chapter8Code {
  26. /**
  27. * @param args
  28. * @throws IOException
  29. */
  30. public static void main(String[] args) throws IOException {
  31. File file = new File("test.txt");
  32. if (!file.exists()) {
  33. file.createNewFile();
  34. }
  35. try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file))) {
  36. Person person = new Person("doctor", "man", 28000);
  37. person.writeExternal(outputStream);
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));) {
  42. Person person2 = new Person();
  43. person2.readExternal(inputStream);
  44. System.out.println(person2);
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. public static class Person implements Externalizable {
  50. private String name;
  51. private String sex;
  52. private Integer age;
  53. public Person() {
  54. // must have
  55. }
  56. public Person(String name, String sex, Integer age) {
  57. this.name = name;
  58. this.sex = sex;
  59. this.age = age;
  60. }
  61. public String getName() {
  62. return name;
  63. }
  64. public void setName(String name) {
  65. this.name = name;
  66. }
  67. public String getSex() {
  68. return sex;
  69. }
  70. public void setSex(String sex) {
  71. this.sex = sex;
  72. }
  73. public Integer getAge() {
  74. return age;
  75. }
  76. public void setAge(Integer age) {
  77. this.age = age;
  78. }
  79. @Override
  80. public String toString() {
  81. return JSON.toJSONString(this);
  82. }
  83. @Override
  84. public void writeExternal(ObjectOutput out) throws IOException {
  85. out.writeUTF(name);
  86. out.writeUTF(sex);
  87. out.writeInt(age);
  88. }
  89. @Override
  90. public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
  91. name = in.readUTF();
  92. sex = in.readUTF();
  93. age = in.readInt();
  94. }
  95. }
  96. }