[源码]ObjectIOStream 对象流 ByteArrayIOStream 数组流 内存流 ZipOutputStream 压缩流
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.Serializable;
- public class ObjectStreamDemo implements Serializable {
- public static void main(String[] args) {
- test3();
- }
- class Person implements Serializable {
- String name = "";
- int score = 0;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getScore() {
- return score;
- }
- public void setScore(int score) {
- this.score = score;
- }
- @Override
- public String toString() {
- return "Person [name=" + name + ", score=" + score + "]";
- }
- }
- /**
- * 向 一个文本文件中写入一个对象
- */
- private static void test1() {
- File file = new File("./PerObjectStoreTxt.txt");
- FileOutputStream fos = null;
- // 创建 ObjectOutputStream 构造器中传入一个OutputStream对象
- ObjectOutputStream oos = null;
- try {
- fos = new FileOutputStream(file);
- // 创建 ObjectOutputStream 构造器中传入一个OutputStream对象
- oos = new ObjectOutputStream(fos);
- oos.writeObject(new ObjectStreamDemo().new Person());
- Person p2 = new ObjectStreamDemo().new Person();
- p2.setName("张三");
- p2.setScore(100);
- oos.writeObject(p2);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- if (oos != null)
- try {
- oos.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- /**
- * 从一个文本文件 读取多个对象
- *
- * @throws
- */
- private static void test2() {
- File file = new File("./PerObjectStoreTxt.txt");
- Person p1;
- Person p2;
- InputStream fis = null;
- ObjectInputStream ois = null;
- try {
- fis = new FileInputStream(file);
- ois = new ObjectInputStream(fis);
- p1 = (Person) ois.readObject();
- p2 = (Person) ois.readObject();
- System.out.println(p1);
- System.out.println(p2);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (ois != null)
- try {
- ois.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 完成 对象的深克隆
- */
- private static void test3() {
- Person p1=new ObjectStreamDemo().new Person();
- String pName =new String("张三");//最终看这个东西的指向是否还一样
- p1.setName(pName);
- /**
- *由此可以看出 ByteArrayIOStream 还是有点用的 主要就是继承IOStream这点
- * 有了这点 这个东西 就分别可以作为 ObjectIIOStream的构造器参数传入
- * 因为 ObjectInputStream(InputStream is) ObjectOutputStream(OutputStream)
- * 他俩就提供 这么 两个东西
- */
- ByteArrayOutputStream baos =null;
- ByteArrayInputStream bais=null;
- ObjectOutputStream oos=null;
- ObjectInputStream ois=null;
- try {
- baos=new ByteArrayOutputStream();
- oos=new ObjectOutputStream(baos);
- oos.writeObject(p1);
- /**
- *如何完成 ByteArrayOutputStream 和ByteArrayInputStream 东西的交互?
- *
- * 实际上这两个东西 只是继承的东西不一样 底层其他基本一样:
- * 实际上 这两个东西 都要 自己手动支取:
- * ByteArrayOutputStream() 写进去数组还能 用toString toByteArray拿出来
- * ByteArrayInputStream(byte[] b) 还需要传byte[]进去 ,可见这个东西古老
- */
- bais=new ByteArrayInputStream(baos.toByteArray());
- ois=new ObjectInputStream(bais);
- //获取深克隆
- Person p2=(Person)ois.readObject();
- System.out.println(p2);
- //修改 克隆对象里边的 成员对象
- p2.name="lisi";
- //看原对象的 成员对象是否更改
- System.out.println(p1);
- //再看 这个被克隆的对象 是否被更改
- System.out.println(p2);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- try {
- oos.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- ois.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- ByteArrayOutputStream 存的是byte[]
- 具有 write到其他OutputStream 的方法
- 还有就是没有CAPACITY
- public class ByteArrayOutputStream extends OutputStream{
- protected byte buf[];
- protected int count;
- //ByteArray写入流有两个 构造器 传入默认的数组容量参数
- //而ByteAarry写出流 因为一旦放进去 就不允许更改,所以只有一个传byte[]
- public ByteArrayOutputStream() {
- this(32);
- }
- public ByteArrayOutputStream(int size) {
- if (size < 0) {
- throw new IllegalArgumentException("Negative initial size: "
- + size);
- }
- buf = new byte[size];
- }
- public synchronized void write(int b) {
- int newcount = count + 1;
- if (newcount > buf.length) {
- buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
- }
- buf[count] = (byte)b;
- count = newcount;
- }
- public synchronized void write(byte b[], int off, int len) {
- if ((off < 0) || (off > b.length) || (len < 0) ||
- ((off + len) > b.length) || ((off + len) < 0)) {
- throw new IndexOutOfBoundsException();
- } else if (len == 0) {
- return;
- }
- int newcount = count + len;
- if (newcount > buf.length) {
- buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
- }
- System.arraycopy(b, off, buf, count, len);
- count = newcount;
- }
- public synchronized void writeTo(OutputStream out) throws IOException {
- //重新 写入新的 输出流 这个类这么一搞算是废了
- out.write(buf, 0, count);
- }
- public synchronized void reset() {
- count = 0;
- }
- public synchronized byte toByteArray()[] { //这写法6
- return Arrays.copyOf(buf, count);
- }
- public synchronized int size() {
- return count;
- }
- public synchronized String toString() {
- return new String(buf, 0, count);
- }
- public synchronized String toString(String charsetName)
- throws UnsupportedEncodingException
- {
- return new String(buf, 0, count, charsetName);
- }
- public synchronized String toString(int hibyte) {
- return new String(buf, hibyte, 0, count);
- }
- //这个 两个流是直接 写入内存的 内存管理 会进行回收处理
- //所以不需要手动关流 这个 close()方法也就是空的
- public void close() throws IOException {
- }
- }
- package ZipOutputStreamDemo;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipOutputStream;
- public class ZipOutputStreamDemo {
- public static void main(String[] args) {
- test1();
- }
- /**
- * 创建 .zip文件 把这个文件封装到ZipFile对象里 直接用ZipOutPutStream 把要写的文件写进去就OK了
- */
- // 把文件 1.txt demo.class 实例.PNG 写入 压缩包 one.zip
- private static void test1() {
- File zipFile = new File("C:/Users/zongjihengfei/Desktop/one.rar/");
- // 无论存在与否 先把它创建出来
- try {
- zipFile.createNewFile();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- FileOutputStream fos = null;
- ZipOutputStream zos = null;
- // 拿到 一个文件
- File file = new File("./1.txt");
- // 用 文件输入流 去获取把 文件内容 读取进来
- FileInputStream fis = null;
- try {
- fos = new FileOutputStream(zipFile);
- // ZipOutputStream(OutputStream os)
- zos = new ZipOutputStream(fos);
- fis = new FileInputStream(file);
- int hasRead = 0;
- /**
- * ZipOutputStream 对象 是由文件条目构成的 也就是说 你每添加一个条目 再往里边写 写的内容就是给这个条目
- *
- * 条目 文件名 在 new ZipEntry(String Name)写上 条目内容 每添加一个条目就写哪个文件
- */
- zos.putNextEntry(new ZipEntry(file.getName())); //首先要增加条目才能往这个条目里边写
- byte[] b = new byte[1024]; //最大吞吐量
- while ((hasRead = fis.read(b)) != -1) {
- zos.write(b, 0, hasRead);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- zos.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- fis.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }


[源码]ObjectIOStream 对象流 ByteArrayIOStream 数组流 内存流 ZipOutputStream 压缩流的更多相关文章
- 菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t[转]
菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn. ...
- JQuery源码之“对象的结构解析”
吃完午饭,觉得有点发困,想起了以后我们的产品可能要做到各种浏览器的兼容于是乎不得不清醒起来!我们的web项目多数是依赖于Jquery的.据了解,在Jquery的2.0版本以后对IE的低端版本浏览器不再 ...
- 读 Runtime 源码:对象与引用计数
以前只是看了很多博客,这次打算看一下源码,并记录下来.想到哪里就读到哪里,写到哪里.读的代码版本是:objc runtime 680,可以从这里下载 https://github.com/RetVal ...
- java源码剖析: 对象内存布局、JVM锁以及优化
一.目录 1.启蒙知识预热:CAS原理+JVM对象头内存存储结构 2.JVM中锁优化:锁粗化.锁消除.偏向锁.轻量级锁.自旋锁. 3.总结:偏向锁.轻量级锁,重量级锁的优缺点. 二.启蒙知识预热 开启 ...
- [python 源码]字符串对象的实现
还是带着问题上路吧,和整数对象的实现同样的问题: >>> a='abc' >>> b='abc' >>> a is b True >> ...
- Netty源码解析 -- 对象池Recycler实现原理
由于在Java中创建一个实例的消耗不小,很多框架为了提高性能都使用对象池,Netty也不例外. 本文主要分析Netty对象池Recycler的实现原理. 源码分析基于Netty 4.1.52 缓存对象 ...
- 从jquery源码中看类型判断和数组的一些操作
在深入看jquery源码中,大家会发现源码写的相当巧妙.那我今天也通过几个源码中用到的技巧来抛砖引玉,希望大家能共同研究源码之精华,不要囫囵吞枣. 1.将类数组转化成数组 我想大家首先想到的方法是fo ...
- Python全栈--9.1--面向对象进阶-super 类对象成员--类属性- 私有属性 查找源码类对象步骤 类特殊成员 isinstance issubclass 异常处理
上一篇文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象 ...
- jQuery源码解析对象实例化与jQuery原型及整体构建模型分析(一)
//源码剖析都基于jQuery-2.0.3版本,主要考虑到兼容IE 一.关于jQuery对象实例化的逻辑: 整个jQuery程序被包裹在一个匿名自执行行数内: (function(window,und ...
随机推荐
- LaTeX_fleqn参数时,多行公式对齐居中的同时选择性的加编号
[转载请注明出处]http://www.cnblogs.com/mashiqi 2016/10/20 一年多没写博文了.今天写一个短的,记录一下使用LaTeX的一些经验. 有些时候,我们的latex文 ...
- 时间改成24小时制 和pc mobile链接自动转化
1 2 <script type ="text/javascript"> function checkserAgent(){ var userAgentInfo=na ...
- Xamarin Android.Views.WindowManagerBadTokenException: Unable to add window -- token android.os.BinderProxy
Android.Views.WindowManagerBadTokenException: Unable to add window -- token android.os.BinderProxy@ ...
- 模拟ajax的同异步
今天突然想到那只在app中,如果请求数据时用的是app提供的接口,如果该接口没有同异步的话,怎么办. 所以就捣腾了下. 模拟ajax同异步. var VshopDataApi = { queryArr ...
- C++ exception
从没用过C++STL中的exception(异常类),在使用rapidxml,操作XML文件时,发现在一个抛出异常的错误.关注了下,就模范着做. 我也专门写了个函数来分配内存,如果发现分配不成功,就抛 ...
- gui2
事件:描述发生了什么的对象. 存在各种不同类型的事件类用来描述各种类型的用户交互. 事件源:事件的产生器. 事件处理器:接收事件.解释事件并处理用户交互的方法. 比如在Button组件上点击鼠标会产生 ...
- 怎么样修改PHPStorm中文件修改后标签和文件名的颜色与背景色
自从最近在PHPstrom里引入Git,并且使用MONOKAI_SUBLIME主题之后 ,当文件在PHPstrom中进行编辑,文档内容变化时,左侧项目文件列表中的文件名颜色以及右侧编辑区域标签卡的文件 ...
- ueditor调用其中的附件上传功能
ueditor实际上是集成了webuploader, 在做内容发布的时候想既有ueditor又有单独的附件上传按钮,这时再加载一个webuploader就显得过于臃肿了,单独利用ueditor的上传功 ...
- Python底层socket库
Python底层socket库将Unix关于网络通信的系统调用对象化处理,是底层函数的高级封装,socket()函数返回一个套接字,它的方法实现了各种套接字系统调用.read与write与Python ...
- java关键字:synchronized
JAVA 如何共享资源 关于synchronized函数: java具有内置机制,可防止某种资源(此处指的是对象的内存内容)冲突.由于你通常会将某class的数据元素声明为private,并且只经由其 ...