歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java對象序列化ObjectOutputStream和ObjectInputStream示例

Java對象序列化ObjectOutputStream和ObjectInputStream示例

日期:2017/3/1 10:12:29   编辑:Linux編程

Java中ObjectInputStream 與 ObjectOutputStream這兩個包裝類可用於輸入流中讀取對象類數據和將對象類型的數據寫入到底層輸入流 。ObjectInputStream 與 ObjectOutputStream 類所讀寫的對象必須實現了 Serializable 接口。需要注意的是:對象中的 transient 和 static 類型的成員變量不會被讀取和寫入 。

具體代碼示例:

O bjectFileConvert.java

  1. package michael.io;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.util.ArrayList;
  9. import java.util.Date;
  10. import java.util.List;
  11. /**
  12. * @blog http://sjsky.iteye.com
  13. * @author Michael
  14. */
  15. public class ObjectFileConvert {
  16. /**
  17. * 文件轉化為Object
  18. * @param fileName
  19. * @return byte[]
  20. */
  21. public static Object file2Object(String fileName) {
  22. FileInputStream fis = null;
  23. ObjectInputStream ois = null;
  24. try {
  25. fis = new FileInputStream(fileName);
  26. ois = new ObjectInputStream(fis);
  27. Object object = ois.readObject();
  28. return object;
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. } finally {
  32. if (fis != null) {
  33. try {
  34. fis.close();
  35. } catch (IOException e1) {
  36. e1.printStackTrace();
  37. }
  38. }
  39. if (ois != null) {
  40. try {
  41. ois.close();
  42. } catch (IOException e2) {
  43. e2.printStackTrace();
  44. }
  45. }
  46. }
  47. return null;
  48. }
  49. /**
  50. * 把Object輸出到文件
  51. * @param obj
  52. * @param outputFile
  53. */
  54. public static void object2File(Object obj, String outputFile) {
  55. ObjectOutputStream oos = null;
  56. FileOutputStream fos = null;
  57. try {
  58. fos = new FileOutputStream(new File(outputFile));
  59. oos = new ObjectOutputStream(fos);
  60. oos.writeObject(obj);
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. } finally {
  64. if (oos != null) {
  65. try {
  66. oos.close();
  67. } catch (IOException e1) {
  68. e1.printStackTrace();
  69. }
  70. }
  71. if (fos != null) {
  72. try {
  73. fos.close();
  74. } catch (IOException e2) {
  75. e2.printStackTrace();
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * @param args
  82. */
  83. @SuppressWarnings("unchecked")
  84. public static void main(String[] args) {
  85. String fileName = "d:/test/object.obj";
  86. List<String> list = new ArrayList<String>();
  87. list.add("michael");
  88. list.add("大大");
  89. ObjectFileConvert.object2File(list, fileName);
  90. System.out.println("success write List<String> to file.");
  91. List<String> tmpList = (List<String>) ObjectFileConvert
  92. .file2Object(fileName);
  93. for (String tmp : tmpList) {
  94. System.out.println(tmp);
  95. }
  96. System.out.println("--------------------------------");
  97. fileName = "d:/test/uservo.obj";
  98. UserVo vo = new UserVo("michael", "大大", 18, new Date());
  99. ObjectFileConvert.object2File(vo, fileName);
  100. System.out.println("success write bean:UserVo to file.");
  101. UserVo tmpvo = (UserVo) ObjectFileConvert.file2Object(fileName);
  102. System.out.println("read bean:UserVo from file get info : " + tmpvo);
  103. }
  104. }

UserVo.java

  1. package michael.io;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. /**
  5. * @blog http://sjsky.iteye.com
  6. * @author Michael
  7. */
  8. public class UserVo implements Serializable {
  9. /**
  10. * serialVersionUID
  11. */
  12. private static final long serialVersionUID = -6846034858002233878L;
  13. private String userId;
  14. private String userName;
  15. private int age;
  16. private Date born;
  17. public UserVo() {
  18. }
  19. public UserVo(String userId, String userName, int age, Date born) {
  20. this.userId = userId;
  21. this.userName = userName;
  22. this.age = age;
  23. this.born = born;
  24. }
  25. /**
  26. * @return the userId
  27. */
  28. public String getUserId() {
  29. return userId;
  30. }
  31. /**
  32. * @return the userName
  33. */
  34. public String getUserName() {
  35. return userName;
  36. }
  37. /**
  38. * @return the age
  39. */
  40. public int getAge() {
  41. return age;
  42. }
  43. /**
  44. * @return the born
  45. */
  46. public Date getBorn() {
  47. return born;
  48. }
  49. /**
  50. * @param pUserId the userId to set
  51. */
  52. public void setUserId(String pUserId) {
  53. userId = pUserId;
  54. }
  55. /**
  56. * @param pUserName the userName to set
  57. */
  58. public void setUserName(String pUserName) {
  59. userName = pUserName;
  60. }
  61. /**
  62. * @param pAge the age to set
  63. */
  64. public void setAge(int pAge) {
  65. age = pAge;
  66. }
  67. /**
  68. * @param pBorn the born to set
  69. */
  70. public void setBorn(Date pBorn) {
  71. born = pBorn;
  72. }
  73. @Override
  74. public String toString() {
  75. return "userId=[ " + userId + " ] userName=[ " + userName + " ] age=[ "
  76. + age + " ] born=[ " + born + "] .";
  77. }
  78. }

運行結果如下:

success write List<String> to file.
michael
大大
--------------------------------
success write bean:UserVo to file.
read bean:UserVo from file get info : userId=[ michael ] userName=[ 大大 ] age=[ 18 ] born=[ Mon Aug 01 13:49:33 CST 2011] .

Copyright © Linux教程網 All Rights Reserved