一、浅谈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. vmstat

    vmstat(virtual memory statitics)命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况, ...

  2. sqlite--代码操作

    1.创建数据库 NSString * docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainM ...

  3. Google map实现类似Google earth的图标展开功能

    前言 在Google map的开发时,如果有多个图标重叠在一起,这时下面的图标就点击不到.而在Google Earth中,鼠标一移到上面就会自动弹开,这对于用户而言是十分人性化的一个功能.如下是在Go ...

  4. C#double保留两位小数

    public static void Main(string[] args) { double db = Math.PI; db = Math.Round(db,); Console.WriteLin ...

  5. 一、webpack那点事-安装、环境搭建

    前言: 还记得两年前刚来公司才几个月,经理就安排我去做JS地图相关的维护和开发工作,然后就跟着一个公司老鸟(没俩月他离职了)熟悉地图相关的功能. 本人嘛,那会前端JS实际开发经验也才几个月,然后当我看 ...

  6. asp.net跨域上传文件

    前端: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" con ...

  7. struts2中<s:select/>标签的运用详解

    <s:select list="smsTypes" listKey="SmsTypeId" listValue="SmsTypeName&quo ...

  8. (我国的省—市—区)三级联动数据库.sql

    # MySQL-Front 5.1  (Build 2.7) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET SQL_MODE='' */ ...

  9. 深入浅出scanf、getcha、gets、cin函数

    转:问题描述一:(分析scanf()和getchar()读取字符) scanf(), getchar()等都是标准输入函数,一般人都会觉得这几个函数非常简单,没什么特殊的.但是有时候却就是因为使用这些 ...

  10. Scala学习笔记--特质trait

    http://outofmemory.cn/scala/scala-trait-introduce-and-example 与Java相似之处 Scala类型系统的基础部分是与Java非常相像的.Sc ...