ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

用IO流复制文件的方法性能比较

2021-11-17 17:32:35  阅读:134  来源: 互联网

标签:性能 System long currentTimeMillis 复制 len IO close new


字节流的读取方法适合读取图片、音频、视频等文件。

字符流的读取方式适合读取大型文本。

我们来对比彼此间的读写效率来进行比较,分别以1.55MB的图片和3M的《剑来》两张文章作位读写对象。

字节流

1、字节流:FileInputStream、FileOutputStream(逐字节读取)

public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("kf.jpg");
        FileOutputStream fos = new FileOutputStream("kf1.jpg");

        //读写操作:
        long s1 = System.currentTimeMillis();
        int len;
        while ((len = fis.read())!=-1){
            fos.write(len);
        }
        fis.close();
        fos.close();
        long e1 = System.currentTimeMillis();
        System.out.println((e1-s1)+"毫秒");
    }

耗时:

 

2、用缓冲流来优化字节流:BufferedFileInputStream、BufferedFileOutputStream(逐字节读取)

public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("kf.jpg");
        FileOutputStream fos = new FileOutputStream("kf1.jpg");
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        //读写操作:
        long s1 = System.currentTimeMillis();
        int len;
        while ((len = bis.read())!=-1){
            bos.write(len);
        }
        bos.close();
        fos.close();
        bis.close();
        fis.close();
        
        long e1 = System.currentTimeMillis();
        System.out.println((e1-s1)+"毫秒");
    }

结果21秒:速度提升了300倍,可见缓冲流的的强大之处。

 

3、给字节流加上缓冲数组:一次读取一个数组的长度

public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("kf.jpg");
        FileOutputStream fos = new FileOutputStream("kf1.jpg");


        //读写操作:
        long s1 = System.currentTimeMillis();
        int len;
        byte[] b = new byte[1024];
        while ((len = fis.read(b))!=-1){
            fos.write(b);
        }
        fis.close();
        fos.close();

        long e1 = System.currentTimeMillis();
        System.out.println((e1-s1)+"毫秒");
    }

速度继续提升,结果1.5M的图片只需要10毫秒

 

4、给缓冲流加上缓冲数组

public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("kf.jpg");
        FileOutputStream fos = new FileOutputStream("kf1.jpg");
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);


        //读写操作:
        long s1 = System.currentTimeMillis();
        int len;
        byte[] b = new byte[1024];
        while ((len = bis.read(b))!=-1){
            bos.write(b,0,len);
        }
        fos.close();
        bos.close();
        fis.close();
        bis.close();

        long e1 = System.currentTimeMillis();
        System.out.println((e1-s1)+"毫秒");
    }

此时缓冲流继续加强,比加上缓冲数组的字节流速度再次提升了5倍,这若是个浩大的工程节约的可不止这一点点速度。

 

字符流:

1、FileWriter、FileReader(按字符读取!)

public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("1234.txt");
        FileReader fr = new FileReader("123.txt");

        long s2 = System.currentTimeMillis();
        int len;
        while((len = fr.read()) != -1){
            fw.write(len);
        }
        fw.close();
        fr.close();
        long e2 = System.currentTimeMillis();
        System.out.println((e2-s2)+"毫秒");
    }

3M的文本文件耗时:

 

2、给字节流加上数组

public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("1234.txt");
        FileReader fr = new FileReader("123.txt");

        long s2 = System.currentTimeMillis();
        char[] c = new char[1024];
        while((fr.read(c)) != -1){
            fw.write(c);
        }
        fw.close();
        fr.close();
        long e2 = System.currentTimeMillis();
        System.out.println((e2-s2)+"毫秒");
    }

结果:

 

3、使用缓冲流的缓冲数组读取。

public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("1234.txt");
        FileReader fr = new FileReader("123.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        BufferedReader br = new BufferedReader(fr);

        long s2 = System.currentTimeMillis();
        char[] c = new char[1024];
        while((br.read(c)) != -1){
            bw.write(c);
            bw.newLine();
        }
        bw.flush();
        fw.close();
        bw.close();
        fr.close();
        br.close();
        long e2 = System.currentTimeMillis();
        System.out.println((e2-s2)+"毫秒");
    }

结果:

 

字节流和字符流读同一个文件:

public static void main(String[] args) throws IOException {
        //字节流
        FileInputStream fis = new FileInputStream("kf.jpg");
        FileOutputStream fos = new FileOutputStream("kf2.jpg");

        //字符流
        FileReader fr = new FileReader("kf.jpg");
        FileWriter fw = new FileWriter("kf3.jpg");

        //缓冲流
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        BufferedReader br = new BufferedReader(fr);
        BufferedWriter bw = new BufferedWriter(fw);
        //字节流读取图片
        long s1 = System.currentTimeMillis();
        int len;
        byte[] b = new byte[1024];
        while((len = bis.read(b))!= -1){//111毫秒
            bos.write(b,0,len);
        }
        long e1 = System.currentTimeMillis();
        System.out.println("字节流:"+(e1-s1)+"毫秒");

        //字符流读图片
        long s2 = System.currentTimeMillis();
        char[] c = new char[1024];
        while(br.read(c)!=-1){
            bw.write(c);
        }
        long e2 = System.currentTimeMillis();
        System.out.println("字符流"+(e2-s2)+"毫秒");

        bw.flush();
        bos.flush();
        bos.close();
        fos.close();
        br.close();
        bw.close();
        fw.close();
        fis.close();
        fr.close();
        bis.close();
    }

结果:用字符流读取图片速度很慢!

 

标签:性能,System,long,currentTimeMillis,复制,len,IO,close,new
来源: https://blog.csdn.net/OKkkbh/article/details/121379672

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有