一、浅谈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. CentOS LiveCD LiveDVD DVD 等版本的区别

    1.CentOS系统镜像DVD有两个,安装系统只用到第一个镜像即CentOS-6.7-x86_64-bin-DVD1.iso,第二个镜像CentOS-6.7-x86_64-bin-DVD2.iso是系 ...

  2. Synopsys逻辑工艺库

    Synopsys逻辑工艺库(DC综合) 逻辑库包括仅与综合过程有关的信息且通过DC用于设计的综合和优化. 这一信息包括引脚到引脚的时序.面积.引脚类型和功耗以及其他DC需要的必须数据. 逻辑库是一个文 ...

  3. css3选择符使用个人理解。

    元素选择符: 通配选择符:一般用* 星号表示  他会命中整体标签. 类型选择符:一般用者空格表示  他会命中标签自己的所有子元素. ID选择符:  一般用# 井号表示  他会命中以id为属性的标签   ...

  4. 腾讯云(centos)上安装apache

    安装 yun install httpd 随系统自启动 chkconfig httpd on 修改/etc/httpd/conf/httpd.conf文件 #修改WWW目录为自己的目录 Dcoumen ...

  5. itext poi 学习之旅 (3)读取数据库信息并由excel展现出来

    DBConnection.java 连接数据库操作 package com.zkbj.poi; import java.sql.Connection; import java.sql.DriverMa ...

  6. uva 10609 - Fractal

    题目大意:给出A,B两个点的坐标,以及T,每次找到A.B的四等分点C,D,然后以AB/2为边长,C,D为顶点,构建一个等边三角形,E为另外一个顶点,然后再对C,E:E,D做同样的操作,直到构建的等边三 ...

  7. js分页算法

    function get_hs_page(cur_page, total_page) { var result = ""; for(var i = 1; i <= total ...

  8. .net framework3.0 以上版本开发的软件乱码问题

    首先介绍一下:WPF是微软新一代图形系统,运行在.NET Framework 3.0及以上版本下,为用户界面.2D/3D 图形.文档和媒体提供了统一的描述和操作方法.基于DirectX 9/10技术的 ...

  9. Google的Guava工具类splitter和apache stringutil对比 编辑

    一直用的是apache的stringutil工具类,其实google的工具类项目 guava中居然也有字符串的分隔类splitter的,在 http://code.google.com/p/guava ...

  10. Ubuntu pip 安装网络爬虫框架 scrapy 出现的错误

    1.问题     File "/usr/bin/pip", line 9, in <module> load_entry_point('pip==1.5.4', 'co ...