一、浅谈io系统

  io系统的结构化思想是:输入-转换流-装饰器-输出。

  对于字节流来说,常见的结构类为:

  

  

package com.handchina.yunmart.middleware.service;

import org.elasticsearch.common.inject.Inject;
import org.junit.Test;

import java.io.*;

/**
 * Created by yq on 2016/11/18.
 */
public class IOTestServiceTest {
    @Inject
    private IOTestService ioTestService;

    @Test
    public void testFile() {
        FileReader br = null;
        FileWriter bw = null;
        try {
            String in = "file/fileDemoIn.txt";
            String out = "file/fileDemoOut.txt";
            br = new FileReader(in);int s;
            Long start1 = System.currentTimeMillis();
            bw = new FileWriter(out);
            while ((s = br.read()) != -1) {
                bw.write(s);
            }
            System.out.println("===========================复制时间:" + (System.currentTimeMillis() - start1) + "毫秒");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testFileDemo() {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            String in = "file/fileDemoIn.txt";
            String out = "file/fileDemoOut.txt";
            br = new BufferedReader(new FileReader(in));
            StringBuilder sb = new StringBuilder();
            String s = null;
            Long start1 = System.currentTimeMillis();
            while ((s = br.readLine()) != null) {
                sb.append(s);
                sb.append("\r\n");
            }
            System.out.println("===========================读取时间:" + (System.currentTimeMillis() - start1) + "毫秒");

            bw = new BufferedWriter(new FileWriter(out));
            Long start2 = System.currentTimeMillis();
            bw.write(sb.toString());
            bw.flush();
            System.out.println("===========================写入时间:" + (System.currentTimeMillis() - start2) + "毫秒");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testByte() {
        FileInputStream bi = null;
        FileOutputStream bo = null;
        try {
            String in = "file/fileStreamIn.avi";
            String out = "file/fileStreamOut.avi";
            bi = new FileInputStream(in);
            bo = new FileOutputStream(out);
            byte[] buf = new byte[1024];
            Long start1 = System.currentTimeMillis();
            int temp = 0;
            while ((temp = bi.read(buf)) != -1) {
                bo.write(buf, 0, temp);
            }
            System.out.println("===========================复制时间:" + (System.currentTimeMillis() - start1) + "毫秒");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bi != null) {
                try {
                    bi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bo != null) {
                try {
                    bo.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testByteDemo() {
        BufferedInputStream bi = null;
        BufferedOutputStream bo = null;
        try {
            String in = "file/fileStreamIn.avi";
            String out = "file/fileStreamOut.avi";
            bi = new BufferedInputStream(new FileInputStream(in));
            bo = new BufferedOutputStream(new FileOutputStream(out));
            byte[] buf = new byte[1024];
            Long start1 = System.currentTimeMillis();
            int temp = 0;
            while ((temp = bi.read(buf)) != -1) {
                bo.write(buf, 0, temp);
            }
            System.out.println("===========================复制时间:" + (System.currentTimeMillis() - start1) + "毫秒");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bi != null) {
                try {
                    bi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bo != null) {
                try {
                    bo.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

io系统的更多相关文章

  1. 从零开始山寨Caffe·拾贰:IO系统(四)

    消费者 回忆:生产者提供产品的接口 在第捌章,IO系统(二)中,生产者DataReader提供了外部消费接口: class DataReader { public: ......... Blockin ...

  2. 从零开始山寨Caffe·陆:IO系统(一)

    你说你学过操作系统这门课?写个无Bug的生产者和消费者模型试试! ——你真的学好了操作系统这门课嘛? 在第壹章,展示过这样图: 其中,左半部分构成了新版Caffe最恼人.最庞大的IO系统. 也是历来最 ...

  3. java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET

    java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了!      社区福利快来领取免费参加MDCC大会机会哦    Tag功能介绍—我们 ...

  4. 彻底明白Java的IO系统

    java学习:彻底明白Java的IO系统 文章来源:互联网 一. Input和Output1. stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源.在Java的IO中,所有 ...

  5. 什么是PROFINET IO系统的实时性

    实时系统是指系统能及时响应外部事件的请求,在规定的时间内完成对该事件的处理,并控制所有实时任务协调一致的运行. PROFINET IO系统的实时性就是指当有一个外部事件发生时,从输入信号到传输.到控制 ...

  6. Java的IO系统

     Java IO系统     "对语言设计人员来说,创建好的输入/输出系统是一项特别困难的任务."     由于存在大量不同的设计方案,所以该任务的困难性是很容易证明的.其中最大的 ...

  7. 【Java基础系列】Java IO系统

    前言 创建好的输入/输出系统不仅要考虑三种不同种类的IO系统(文件,控制台,网络连接)还需要通过大量不同的方式与他们通信(顺序,随机访问,二进制,字符,按行,按字等等). 一.输入和输出 Java的I ...

  8. Atitit.软件开发概念说明--io系统区--特殊文件名称保存最佳实践文件名称编码...filenameEncode

    Atitit.软件开发概念说明--io系统区--特殊文件名称保存最佳实践文件名称编码...filenameEncode 不个网页title保存成个个文件的时候儿有无效字符的问题... 通常两个处理方式 ...

  9. Qt-QML-C++交互实现文件IO系统

    QMl是没有自己的文件IO控制的,这里如果我们需要对文件进行读写操作,那么就需要去C++或者JS完成交互,交互方式有多种,由于我还没有掌握,这里就不介绍具体的交互方式了.这里就简单说明一下我的实现过程 ...

随机推荐

  1. Word01-从正文处开始插入页码

    一份正式的文档应该由以下几部分组成:封面.目录.摘要.正文…… 现在要求前三页不需要插入页码,从正文部分插入页码为第一页,原文档如下: 步骤: 第一步:将光标移动到摘要页的末尾,选择页面部局--> ...

  2. 广播接收者 BroadcastReceiver 示例-2

    BaseActivity /**所有Activity的基类*/ public class BaseActivity extends Activity {     @Override     prote ...

  3. C# 无边框窗体移动代码

    C# 无边框窗体移动代码 Point _frmPoint = new Point(); //移动前窗体左上角坐标 Point _mousePoint = new Point(); //按下鼠标时坐标 ...

  4. Lesson 2: Dive Into Typography (排版)

    Lesson 2: Dive Into Typography (排版) 排版是字的艺术,是关于字的一切:字体.字号.行高.行长.字重(斜体/加粗/正常).字距(kerning).行距(leading) ...

  5. seajs原理解析

    一: 1.本文是基于seajs2.2.1编写的,之后版本应该大同小异 2.本文仅代表个人观点,如有理解错误,敬请指出,大家一起学习 二: 1.首先放一张我画的流程图 这是我理解的seajs的基本的所有 ...

  6. VC实用小知识总结 (一),转http://blog.csdn.net/myiszjf/article/details/10007431

    在上一篇中,我们以经介绍了程序的流程和框架,在本篇将详细讨论各个功能的实现主要包括 1.获取磁盘信息2.获取目录信息3.获取文件信息4.运行指定文件5.删除指定文件6.删除指定目录7.创建指定目录8. ...

  7. jquery实现复选框全选反选

    实现原理: 给所有的复选框取相同的名字,当点击全选的时候把chenked属性全部设置为true;当点击全不选的时候把checked属性设置为false; 源代码如下: html代码: <form ...

  8. 1. Server.Transfer和Response.Redirect

    今天在使用ServerTransfer和Response.Redirect定位到当前页面来实现刷新页面时,发现了一些现象: 1.使用Response.Redirect刷新本页面,造成当前页面显示的数据 ...

  9. Ubuntu12.04下载Android4.0.1源码全过程,附若干问题解决[转]

    学校里一直在做应用层开发,考虑到日后就业问题,这次决定研究源码和驱动,并进行编译.没想到就下载源码这一步折腾了我整整两天,期间遇到很多问题,哎,记录于此,希望日后再下源码的人不要再走无谓的弯路了.事实 ...

  10. 【Xamarin-IOS 开发环境搭建】

    MAC 比较贵,虚拟机的干活..... 配置完虚拟机.进去后 ,安装XCode的 时候 ,失败了.错误信息: But when i have tried to install the Xcode it ...