思路:把已经序列化的对象存入容器(如LinkedList<?>)中,然后用ObjectInputStream和ObjectOutputStream对这个实例化的LinkedList<?>对象进行读写。

  1. 1 /**
  2. 2 * @Title: FileRW.java
  3. 3 * @Package com.file
  4. 4 * @Description: 文件、文件夹的创建、写入练习。读写是使用对象流实现。
  5. 5 * @author 慢跑学Android
  6. 6 * @date 2011-11-19 下午03:53:01
  7. 7 * @version V1.0
  8. 8 */
  9. 9 package com.file;
  10. 10
  11. 11 import java.io.File;
  12. 12 import java.io.FileInputStream;
  13. 13 import java.io.FileNotFoundException;
  14. 14 import java.io.FileOutputStream;
  15. 15 import java.io.IOException;
  16. 16 import java.io.ObjectInputStream;
  17. 17 import java.io.ObjectOutputStream;
  18. 18 import java.util.LinkedList;
  19. 19
  20. 20
  21. 21 public class FileRW {
  22. 22 private String dirPath;
  23. 23 private String filename;
  24. 24
  25. 25 public static void main(String[] args) {
  26. 26 String path = "C:\\晓声";
  27. 27 String fileName = "test.txt";
  28. 28 FileRW fileRW = new FileRW(path, fileName);
  29. 29 LinkedList<TestMessage> msgOut = new LinkedList<TestMessage>();
  30. 30 LinkedList<TestMessage> msgIn = null;
  31. 31
  32. 32 msgOut.add(new TestMessage("柯南", "偶像"));
  33. 33 msgOut.add(new TestMessage("卡卡西", "好样的"));
  34. 34 msgOut.add(new TestMessage("Android", "Android"));
  35. 35 msgOut.add(new TestMessage("哈哈", "测试下喔"));
  36. 36 fileRW.writeObject(path, fileName, msgOut);
  37. 37
  38. 38 msgIn = fileRW.readObject(path,fileName);
  39. 39
  40. 40 for(TestMessage temp:msgIn) {
  41. 41 System.out.println(temp.getName() + temp.getData());
  42. 42 }
  43. 43
  44. 44 }
  45. 45
  46. 46 public FileRW(String dirPath, String filename) {
  47. 47 this.dirPath = dirPath;
  48. 48 this.filename = filename;
  49. 49 if (creatDir()) {
  50. 50 creatFile();
  51. 51 }
  52. 52 }
  53. 53
  54. 54
  55. 55 private boolean creatDir() {
  56. 56 if (null != dirPath) {
  57. 57 File path = new File(dirPath);
  58. 58 if (path.exists()) {
  59. 59 return true;
  60. 60 }
  61. 61 if (true == path.mkdirs() ) {
  62. 62 return true;
  63. 63 }
  64. 64 }
  65. 65 return false;
  66. 66 }
  67. 67
  68. 68 private void creatFile() {
  69. 69 if (null != filename) {
  70. 70 File file = new File(dirPath, filename);
  71. 71 if (false == file.exists()) {
  72. 72 try {
  73. 73 file.createNewFile();
  74. 74 } catch (IOException e) {
  75. 75 e.printStackTrace();
  76. 76 }
  77. 77 }
  78. 78 }
  79. 79 }
  80. 80
  81. 81
  82. 82 /**
  83. 83 * @Title: writeObject
  84. 84 * @Description: Write a object to a file.
  85. 85 * @param path the directory of the target file
  86. 86 * @param filename the name of the target file
  87. 87 * @param msg the type of the object
  88. 88 * @return void
  89. 89 * @throws
  90. 90 */
  91. 91 private void writeObject(String path, String filename, LinkedList<TestMessage> msg) {
  92. 92 File file = new File(path, filename);
  93. 93 if (false == file.isFile()) {
  94. 94 return ;
  95. 95 }
  96. 96
  97. 97 try {
  98. 98 // The value "false" for FileOutputStream means that overwrite this file,
  99. 99 // if it is "true",append the new data to this file.
  100. 100 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file,false));
  101. 101 oos.writeObject(msg);
  102. 102 oos.flush();
  103. 103 oos.close();
  104. 104 } catch (FileNotFoundException e) {
  105. 105 e.printStackTrace();
  106. 106 } catch (IOException e) {
  107. 107 e.printStackTrace();
  108. 108 }
  109. 109 }
  110. 110
  111. 111 /**
  112. 112 * @Title: readObject
  113. 113 * @Description: Read a object from a file.
  114. 114 * @param path the directory of the target file
  115. 115 * @param filename the name of the target file
  116. 116 * @return LinkedList<TestMessage>
  117. 117 * @throws
  118. 118 */
  119. 119 @SuppressWarnings("unchecked")
  120. 120 private LinkedList<TestMessage> readObject(String path, String filename) {
  121. 121 File file = new File(path, filename);
  122. 122 ObjectInputStream ois = null;
  123. 123 LinkedList<TestMessage> msgAll = null;
  124. 124
  125. 125 try {
  126. 126 ois = new ObjectInputStream(new FileInputStream(file));
  127. 127 try {
  128. 128 msgAll = (LinkedList<TestMessage>)ois.readObject();
  129. 129
  130. 130 } catch (ClassNotFoundException e) {
  131. 131 e.printStackTrace();
  132. 132 }
  133. 133 } catch (FileNotFoundException e) {
  134. 134 e.printStackTrace();
  135. 135 } catch (IOException e) {
  136. 136 e.printStackTrace();
  137. 137 } finally {
  138. 138 try {
  139. 139 ois.close();
  140. 140 } catch (IOException e) {
  141. 141 e.printStackTrace();
  142. 142 }
  143. 143 }
  144. 144
  145. 145 return msgAll;
  146. 146 }
  147. 147 }
  1. 1 /**
  2. 2 * @Title: TestMessage.java
  3. 3 * @Package com.file
  4. 4 * @Description: FileRW的消息流
  5. 5 * @author 慢跑学Android
  6. 6 * @date 2011-11-19 下午04:35:11
  7. 7 * @version V1.0
  8. 8 */
  9. 9 package com.file;
  10. 10
  11. 11
  12. 12 public class TestMessage implements java.io.Serializable {
  13. 13 private String name;
  14. 14 private String data;
  15. 15
  16. 16 public String getName() {
  17. 17 return name;
  18. 18 }
  19. 19 public void setName(String name) {
  20. 20 this.name = name;
  21. 21 }
  22. 22 public String getData() {
  23. 23 return data;
  24. 24 }
  25. 25 public void setData(String data) {
  26. 26 this.data = data;
  27. 27 }
  28. 28 public TestMessage(String name, String msg) {
  29. 29 this.name = name;
  30. 30 data = msg;
  31. 31 }
  32. 32 }

Java 如何对文件进行多个Object对象流的读写操作的更多相关文章

  1. JavaScript:对Object对象的一些常用操作总结

    JavaScript对Object对象的一些常用操作总结. 一.Object.assign() 1.可以用作对象的复制 var obj = { a: 1 }; var copy = Object.as ...

  2. java实现xml文件读取并保存到对象

    首先浅聊一下解析xml的四种方式: 1.DOM方式:有缺点但是这个缺点却也是他的优点.下面详细介绍: 以树形的层次结构组织节点或信息片断集合,可以获得同一个文档中的多处不同数据.使用起来简单. 优点是 ...

  3. js中对Object对象的一些常用操作总结

    前言我前面的文章,写过js中“类”与继承的一些文章.ES5我们可以通过 构造函数 或者 Object.create()等方式来模拟出js中的“类”,当然,对象呢是类的实例化,我们可以通过如下方式创建对 ...

  4. java对象流(二)

    对象流,可以将java中的对象转为字节进行输出.将对象写入文件时.文件输出流是将字节写入到文件中. 对象流是将给定的对象转化为一组字节.writeObject()方法就是将对象转为字节. 对象流,读的 ...

  5. java处理Excel文件---excel文件的创建,删除,写入,读取

    这篇文章的代码是我封装的excel处理类,包含推断excel是否存在,表格索引是否存在,创建excel文件,删除excel文件,往excel中写入信息,从excel中读取数据. 尤其在写入与读取两个方 ...

  6. java文件的读写操作

    java文件的读写操作主要是对输入流和输出流的操作,由于流的分类很多,所以概念很容易模糊,基于此,对于流的读写操作做一个小结. 1.根据数据的流向来分: 输出流:是用来写数据的,是由程序(内存)--- ...

  7. Java 的字节流文件读取(一)

    上篇文章我们介绍了抽象化磁盘文件的 File 类型,它仅仅用于抽象化描述一个磁盘文件或目录,却不具备访问和修改一个文件内容的能力. Java 的 IO 流就是用于读写文件内容的一种设计,它能完成将磁盘 ...

  8. 对象流,它们是一对高级流,负责即将java对象与字节之间在读写的过程中进行转换。 * java.io.ObjectOutputStream * java.io.ObjectInputStream

    package seday06; import java.io.Serializable;import java.util.Arrays; /** * @author xingsir * 使用当前类来 ...

  9. C# 运用StreamReader类和StreamWriter类实现文件的读写操作

    对文件的读写操作应该是最重要的文件操作,System.IO命名空间为我们提供了诸多文件读写操作类,在这里我要向大家介绍最常用也是最基本的StreamReader类和StreamWriter类.从这两个 ...

随机推荐

  1. Superior Scheduler:带你了解FusionInsight MRS的超级调度器

    摘要:Superior Scheduler是一个专门为Hadoop YARN分布式资源管理系统设计的调度引擎,是针对企业客户融合资源池,多租户的业务诉求而设计的高性能企业级调度器. 本文分享自华为云社 ...

  2. 从工具、工具箱到数字化软件工厂——DevOps 设计理念与工程实践专场 | CIF 精彩看点

    西方经典管理理论认为,组织效率可以归为劳动效率.组织效率和人的效率.美国管理学家泰勒所著的<科学管理原理>被德鲁克誉为"20 世纪最伟大的发明",劳动效率说认为分工提升 ...

  3. Oracle基础命令操作总结

    第1章 oracle命令集 1.1 重启数据库 1.1.1 启动数据库 su -root                 切换到oracle安装用户下,我的是root source .bash_pro ...

  4. 数值分析:幂迭代和PageRank算法

    1. 幂迭代算法(简称幂法) (1) 占优特征值和占优特征向量 已知方阵\(\bm{A} \in \R^{n \times n}\), \(\bm{A}\)的占优特征值是量级比\(\bm{A}\)所有 ...

  5. Python | JSON 数据解析(Json & JsonPath)

    一.什么是JSON? JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.它基于 ECMAScript (欧洲计算机协会制定的js规范)的一 ...

  6. kvm安装window系统及使用NFS动态迁移

    验证是否开启虚拟化 # grep -E 'svm|vmx' /proc/cpuinfo - vmx is for Intel processors - svm is for AMD processor ...

  7. 图解Redis6中的9种数据结构,墙裂建议准备去面试的人先看(干货,建议收藏)

    如图所示,Redis中提供了9种不同的数据操作类型,他们分别代表了不同的数据存储结构. 图2-17 数据类型 String类型 String类型是Redis用的较多的一个基本类型,也是最简单的一种类型 ...

  8. .jar文件没有Java(TM) Platform SE binary打开方式解决办法

    下面是我个人在打开.jar文件时候的一些小问题: 明明已经配置好了环境变量.jar文件却没有 Java(TM) Platform SE binary 的打开方式, 网上查了资料点明是环境变量的问题,后 ...

  9. 初学python-day9 函数1(已更新)

    函数 一.函数基础 1.什么是函数 在一个完整的项目中,某些功能会被重复使用,那么会将代码段封装成函数,当我们要使用的时候,直接调用即可. 函数是可以实现一定的小程序或者功能. 优点: 增加了代码的重 ...

  10. RabbitMQ延时队列应用场景

    应用场景 我们系统未付款的订单,超过一定时间后,需要系统自动取消订单并释放占有物品 常用的方案 就是利用Spring schedule定时任务,轮询检查数据库 但是会消耗系统内存,增加了数据库的压力. ...