1.对象流
  1. import java.io.ByteArrayInputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.ObjectInputStream;
  9. import java.io.ObjectOutputStream;
  10. import java.io.Serializable;
  11.  
  12. public class ObjectStreamDemo implements Serializable {
  13. public static void main(String[] args) {
  14. test3();
  15. }
  16.  
  17. class Person implements Serializable {
  18. String name = "";
  19. int score = 0;
  20.  
  21. public String getName() {
  22. return name;
  23. }
  24.  
  25. public void setName(String name) {
  26. this.name = name;
  27. }
  28.  
  29. public int getScore() {
  30. return score;
  31. }
  32.  
  33. public void setScore(int score) {
  34. this.score = score;
  35. }
  36.  
  37. @Override
  38. public String toString() {
  39. return "Person [name=" + name + ", score=" + score + "]";
  40. }
  41.  
  42. }
  43.  
  44. /**
  45. * 向 一个文本文件中写入一个对象
  46. */
  47. private static void test1() {
  48.  
  49. File file = new File("./PerObjectStoreTxt.txt");
  50. FileOutputStream fos = null;
  51.  
  52. // 创建 ObjectOutputStream 构造器中传入一个OutputStream对象
  53. ObjectOutputStream oos = null;
  54. try {
  55. fos = new FileOutputStream(file);
  56.  
  57. // 创建 ObjectOutputStream 构造器中传入一个OutputStream对象
  58. oos = new ObjectOutputStream(fos);
  59.  
  60. oos.writeObject(new ObjectStreamDemo().new Person());
  61.  
  62. Person p2 = new ObjectStreamDemo().new Person();
  63. p2.setName("张三");
  64. p2.setScore(100);
  65.  
  66. oos.writeObject(p2);
  67.  
  68. } catch (IOException e) {
  69. // TODO Auto-generated catch block
  70. e.printStackTrace();
  71. } finally {
  72. if (oos != null)
  73. try {
  74. oos.close();
  75. } catch (IOException e) {
  76. // TODO Auto-generated catch block
  77. e.printStackTrace();
  78. }
  79.  
  80. }
  81. }
  82.  
  83. /**
  84. * 从一个文本文件 读取多个对象
  85. *
  86. * @throws
  87. */
  88. private static void test2() {
  89. File file = new File("./PerObjectStoreTxt.txt");
  90. Person p1;
  91. Person p2;
  92. InputStream fis = null;
  93. ObjectInputStream ois = null;
  94. try {
  95. fis = new FileInputStream(file);
  96. ois = new ObjectInputStream(fis);
  97.  
  98. p1 = (Person) ois.readObject();
  99. p2 = (Person) ois.readObject();
  100. System.out.println(p1);
  101. System.out.println(p2);
  102. } catch (ClassNotFoundException e) {
  103. e.printStackTrace();
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. } finally {
  107. if (ois != null)
  108. try {
  109. ois.close();
  110. } catch (IOException e) {
  111. e.printStackTrace();
  112. }
  113.  
  114. }
  115. }
  116. /**
  117. * 完成 对象的深克隆
  118. */
  119. private static void test3() {
  120.  
  121. Person p1=new ObjectStreamDemo().new Person();
  122. String pName =new String("张三");//最终看这个东西的指向是否还一样
  123. p1.setName(pName);
  124. /**
  125. *由此可以看出 ByteArrayIOStream 还是有点用的 主要就是继承IOStream这点
  126. * 有了这点 这个东西 就分别可以作为 ObjectIIOStream的构造器参数传入
  127. * 因为 ObjectInputStream(InputStream is) ObjectOutputStream(OutputStream)
  128. * 他俩就提供 这么 两个东西
  129. */
  130. ByteArrayOutputStream baos =null;
  131. ByteArrayInputStream bais=null;
  132. ObjectOutputStream oos=null;
  133. ObjectInputStream ois=null;
  134.  
  135. try {
  136. baos=new ByteArrayOutputStream();
  137. oos=new ObjectOutputStream(baos);
  138.  
  139. oos.writeObject(p1);
  140. /**
  141. *如何完成 ByteArrayOutputStream 和ByteArrayInputStream 东西的交互?
  142. *
  143. * 实际上这两个东西 只是继承的东西不一样 底层其他基本一样:
  144. * 实际上 这两个东西 都要 自己手动支取:
  145. * ByteArrayOutputStream() 写进去数组还能 用toString toByteArray拿出来
  146. * ByteArrayInputStream(byte[] b) 还需要传byte[]进去 ,可见这个东西古老
  147. */
  148.  
  149. bais=new ByteArrayInputStream(baos.toByteArray());
  150. ois=new ObjectInputStream(bais);
  151.  
  152. //获取深克隆
  153. Person p2=(Person)ois.readObject();
  154. System.out.println(p2);
  155. //修改 克隆对象里边的 成员对象
  156. p2.name="lisi";
  157. //看原对象的 成员对象是否更改
  158. System.out.println(p1);
  159. //再看 这个被克隆的对象 是否被更改
  160. System.out.println(p2);
  161. } catch (IOException e) {
  162. // TODO Auto-generated catch block
  163. e.printStackTrace();
  164. } catch (ClassNotFoundException e) {
  165. // TODO Auto-generated catch block
  166. e.printStackTrace();
  167. }finally{
  168.  
  169. try {
  170. oos.close();
  171. } catch (IOException e) {
  172. // TODO Auto-generated catch block
  173. e.printStackTrace();
  174. }
  175. try {
  176. ois.close();
  177. } catch (IOException e) {
  178. // TODO Auto-generated catch block
  179. e.printStackTrace();
  180. }
  181.  
  182. }
  183.  
  184. }
  185. }
 
 
2.内存流 /数组流  :
     其实就是一个纯数组操作!
这个东西 和AbstractStringBuilder 区别不大 ,就这么3个区别:
  1. ByteArrayOutputStream  存的是byte[]
  2. 具有 write到其他OutputStream 的方法
  3. 还有就是没有CAPACITY
 
  1. public class ByteArrayOutputStream extends OutputStream{
  2.  
  3. protected byte buf[];
  4. protected int count;
  5.  
  6. //ByteArray写入流有两个 构造器 传入默认的数组容量参数
  7. //而ByteAarry写出流 因为一旦放进去 就不允许更改,所以只有一个传byte[]
  8. public ByteArrayOutputStream() {
  9. this(32);
  10. }
  11. public ByteArrayOutputStream(int size) {
  12. if (size < 0) {
  13. throw new IllegalArgumentException("Negative initial size: "
  14. + size);
  15. }
  16. buf = new byte[size];
  17. }
  18.  
  19. public synchronized void write(int b) {
  20. int newcount = count + 1;
  21. if (newcount > buf.length) {
  22. buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
  23. }
  24. buf[count] = (byte)b;
  25. count = newcount;
  26. }
  27.  
  28. public synchronized void write(byte b[], int off, int len) {
  29. if ((off < 0) || (off > b.length) || (len < 0) ||
  30. ((off + len) > b.length) || ((off + len) < 0)) {
  31. throw new IndexOutOfBoundsException();
  32. } else if (len == 0) {
  33. return;
  34. }
  35. int newcount = count + len;
  36. if (newcount > buf.length) {
  37. buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
  38. }
  39. System.arraycopy(b, off, buf, count, len);
  40. count = newcount;
  41. }
  42.  
  43. public synchronized void writeTo(OutputStream out) throws IOException {
  44.  
  45. //重新 写入新的 输出流 这个类这么一搞算是废了
  46. out.write(buf, 0, count);
  47. }
  48.  
  49. public synchronized void reset() {
  50. count = 0;
  51. }
  52. public synchronized byte toByteArray()[] { //这写法6
  53. return Arrays.copyOf(buf, count);
  54. }
  55. public synchronized int size() {
  56. return count;
  57. }
  58.  
  59. public synchronized String toString() {
  60. return new String(buf, 0, count);
  61. }
  62.  
  63. public synchronized String toString(String charsetName)
  64. throws UnsupportedEncodingException
  65. {
  66. return new String(buf, 0, count, charsetName);
  67. }
  68.  
  69. public synchronized String toString(int hibyte) {
  70. return new String(buf, hibyte, 0, count);
  71. }
  72.  
  73. //这个 两个流是直接 写入内存的 内存管理 会进行回收处理
  74. //所以不需要手动关流 这个 close()方法也就是空的
  75. public void close() throws IOException {
  76. }
  77.  
  78. }
  所以说这个东西最大的作用就是ByteArrayOutputStream 和ByteArrayInputStream 分别是OutputStream 和InputStream 的子类 如果要用到对象流传进去会方便一些。
 
 
如果说要把 ByteArrayOutputStream的东西拿出来 两招 直接toString() 或者说 getBytes()
 
3.压缩流
压缩流 是用来 把一个文件 写进一个压缩包里。
这个流 不在 io包下 它是在 java.util.zip
如果我们要传输一个较大的文件 那么就要使用压缩技术
 
 
简单传达一下 这里边的东西 :就这么三个东西
 
 
ZipInputStream 解压工具
 
ZipOutputStream 压缩工具
 
ZipEntry代表的是压缩包中的一个文件实体。
 
 
如何压缩文件
直接用ZipIO流去包装IO流
比如说直接用ZipOutputStream 去包装 FileOutputStream 对象
 
 
  1. package ZipOutputStreamDemo;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.util.zip.ZipEntry;
  8. import java.util.zip.ZipOutputStream;
  9.  
  10. public class ZipOutputStreamDemo {
  11. public static void main(String[] args) {
  12. test1();
  13. }
  14.  
  15. /**
  16. * 创建 .zip文件 把这个文件封装到ZipFile对象里 直接用ZipOutPutStream 把要写的文件写进去就OK了
  17. */
  18. // 把文件 1.txt demo.class 实例.PNG 写入 压缩包 one.zip
  19. private static void test1() {
  20. File zipFile = new File("C:/Users/zongjihengfei/Desktop/one.rar/");
  21. // 无论存在与否 先把它创建出来
  22. try {
  23. zipFile.createNewFile();
  24. } catch (IOException e1) {
  25. e1.printStackTrace();
  26. }
  27. FileOutputStream fos = null;
  28. ZipOutputStream zos = null;
  29.  
  30. // 拿到 一个文件
  31. File file = new File("./1.txt");
  32. // 用 文件输入流 去获取把 文件内容 读取进来
  33. FileInputStream fis = null;
  34.  
  35. try {
  36. fos = new FileOutputStream(zipFile);
  37. // ZipOutputStream(OutputStream os)
  38. zos = new ZipOutputStream(fos);
  39. fis = new FileInputStream(file);
  40. int hasRead = 0;
  41.  
  42. /**
  43. * ZipOutputStream 对象 是由文件条目构成的 也就是说 你每添加一个条目 再往里边写 写的内容就是给这个条目
  44. *
  45. * 条目 文件名 在 new ZipEntry(String Name)写上 条目内容 每添加一个条目就写哪个文件
  46. */
  47. zos.putNextEntry(new ZipEntry(file.getName())); //首先要增加条目才能往这个条目里边写
  48.  
  49. byte[] b = new byte[1024]; //最大吞吐量
  50. while ((hasRead = fis.read(b)) != -1) {
  51. zos.write(b, 0, hasRead);
  52. }
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. } finally {
  56.  
  57. try {
  58. zos.close();
  59. } catch (IOException e) {
  60. // TODO Auto-generated catch block
  61. e.printStackTrace();
  62. }
  63. try {
  64. fis.close();
  65. } catch (IOException e) {
  66. // TODO Auto-generated catch block
  67. e.printStackTrace();
  68. }
  69.  
  70. }
  71.  
  72. }
  73. }
 
 
 
 
 
 
 
 
 
 
 
 

[源码]ObjectIOStream 对象流 ByteArrayIOStream 数组流 内存流 ZipOutputStream 压缩流的更多相关文章

  1. 菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t[转]

    菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn. ...

  2. JQuery源码之“对象的结构解析”

    吃完午饭,觉得有点发困,想起了以后我们的产品可能要做到各种浏览器的兼容于是乎不得不清醒起来!我们的web项目多数是依赖于Jquery的.据了解,在Jquery的2.0版本以后对IE的低端版本浏览器不再 ...

  3. 读 Runtime 源码:对象与引用计数

    以前只是看了很多博客,这次打算看一下源码,并记录下来.想到哪里就读到哪里,写到哪里.读的代码版本是:objc runtime 680,可以从这里下载 https://github.com/RetVal ...

  4. java源码剖析: 对象内存布局、JVM锁以及优化

    一.目录 1.启蒙知识预热:CAS原理+JVM对象头内存存储结构 2.JVM中锁优化:锁粗化.锁消除.偏向锁.轻量级锁.自旋锁. 3.总结:偏向锁.轻量级锁,重量级锁的优缺点. 二.启蒙知识预热 开启 ...

  5. [python 源码]字符串对象的实现

    还是带着问题上路吧,和整数对象的实现同样的问题: >>> a='abc' >>> b='abc' >>> a is b True >> ...

  6. Netty源码解析 -- 对象池Recycler实现原理

    由于在Java中创建一个实例的消耗不小,很多框架为了提高性能都使用对象池,Netty也不例外. 本文主要分析Netty对象池Recycler的实现原理. 源码分析基于Netty 4.1.52 缓存对象 ...

  7. 从jquery源码中看类型判断和数组的一些操作

    在深入看jquery源码中,大家会发现源码写的相当巧妙.那我今天也通过几个源码中用到的技巧来抛砖引玉,希望大家能共同研究源码之精华,不要囫囵吞枣. 1.将类数组转化成数组 我想大家首先想到的方法是fo ...

  8. Python全栈--9.1--面向对象进阶-super 类对象成员--类属性- 私有属性 查找源码类对象步骤 类特殊成员 isinstance issubclass 异常处理

    上一篇文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象 ...

  9. jQuery源码解析对象实例化与jQuery原型及整体构建模型分析(一)

    //源码剖析都基于jQuery-2.0.3版本,主要考虑到兼容IE 一.关于jQuery对象实例化的逻辑: 整个jQuery程序被包裹在一个匿名自执行行数内: (function(window,und ...

随机推荐

  1. LaTeX_fleqn参数时,多行公式对齐居中的同时选择性的加编号

    [转载请注明出处]http://www.cnblogs.com/mashiqi 2016/10/20 一年多没写博文了.今天写一个短的,记录一下使用LaTeX的一些经验. 有些时候,我们的latex文 ...

  2. 时间改成24小时制 和pc mobile链接自动转化

    1 2  <script type ="text/javascript"> function checkserAgent(){ var userAgentInfo=na ...

  3. Xamarin Android.Views.WindowManagerBadTokenException: Unable to add window -- token android.os.BinderProxy

    Android.Views.WindowManagerBadTokenException: Unable to add window -- token android.os.BinderProxy@ ...

  4. 模拟ajax的同异步

    今天突然想到那只在app中,如果请求数据时用的是app提供的接口,如果该接口没有同异步的话,怎么办. 所以就捣腾了下. 模拟ajax同异步. var VshopDataApi = { queryArr ...

  5. C++ exception

    从没用过C++STL中的exception(异常类),在使用rapidxml,操作XML文件时,发现在一个抛出异常的错误.关注了下,就模范着做. 我也专门写了个函数来分配内存,如果发现分配不成功,就抛 ...

  6. gui2

    事件:描述发生了什么的对象. 存在各种不同类型的事件类用来描述各种类型的用户交互. 事件源:事件的产生器. 事件处理器:接收事件.解释事件并处理用户交互的方法. 比如在Button组件上点击鼠标会产生 ...

  7. 怎么样修改PHPStorm中文件修改后标签和文件名的颜色与背景色

    自从最近在PHPstrom里引入Git,并且使用MONOKAI_SUBLIME主题之后 ,当文件在PHPstrom中进行编辑,文档内容变化时,左侧项目文件列表中的文件名颜色以及右侧编辑区域标签卡的文件 ...

  8. ueditor调用其中的附件上传功能

    ueditor实际上是集成了webuploader, 在做内容发布的时候想既有ueditor又有单独的附件上传按钮,这时再加载一个webuploader就显得过于臃肿了,单独利用ueditor的上传功 ...

  9. Python底层socket库

    Python底层socket库将Unix关于网络通信的系统调用对象化处理,是底层函数的高级封装,socket()函数返回一个套接字,它的方法实现了各种套接字系统调用.read与write与Python ...

  10. java关键字:synchronized

    JAVA 如何共享资源 关于synchronized函数: java具有内置机制,可防止某种资源(此处指的是对象的内存内容)冲突.由于你通常会将某class的数据元素声明为private,并且只经由其 ...