Java FileOutputStream/BufferedOutputStrram 读入写出效率比较

xuewuhe · 2021年03月26日 · 1361 次阅读

FileOutputStream.write() 方法一共有三种使用方式,其运行效率是 write(byte[]) 效率 > write()

import java.io.*;

public class IOtest {
    public void test(String path){
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            fileOutputStream = new FileOutputStream("e:\\copy1.jpg", true);
            //byte[] bytes = new byte[256];
            int i;
            while ((i = fileInputStream.read())!= -1){
                fileOutputStream.write(i);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void test2(String path){
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            fileOutputStream = new FileOutputStream("e:\\copy2.jpg", true);
            byte[] bytes = new byte[256];
            int i;
            while ((i = fileInputStream.read(bytes))> 0){
                fileOutputStream.write(bytes);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void test3(String path){
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            fileOutputStream = new FileOutputStream("e:\\copy2.jpg", true);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            //byte[] bytes = new byte[256];
            int i;
            while ((i = fileInputStream.read()) != -1){
                bufferedOutputStream.write(i);
                bufferedOutputStream.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        IOtest iOtest = new IOtest();
        long starttime = System.currentTimeMillis();
        iOtest.test("d:\\compress.jpg");
        long endtime = System.currentTimeMillis();
        long time = endtime -starttime;
        System.out.println("fileoutputstream写入共耗时:" + time);


        long starttime1 = System.currentTimeMillis();
        iOtest.test2("d:\\compress1.jpg");
        long endtime1 = System.currentTimeMillis();
        long time1 = endtime1 -starttime1;
        System.out.println("buffered写入共耗时:" + time1);
    }
}

copy 文件的时候,采用 byte[] 读入跟写入时间比较:
fileoutputstream 写入共耗时:7919
buffered 写入共耗时:41
可以看到 buffered 的耗时是 fileoutputstream 耗时的千分之五,这个效率提升是非常明显了。

有人认为 bufferedOutputStream 包装下 outputStream 不是性能更好,真的是这样吗?我们更改下原来的代码:

import java.io.*;

public class IOtest {
    public void test(String path){
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            fileOutputStream = new FileOutputStream("e:\\copy1.jpg", true);
            //byte[] bytes = new byte[256];
            int i;
            while ((i = fileInputStream.read())!= -1){
                fileOutputStream.write(i);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void test2(String path){
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            fileOutputStream = new FileOutputStream("e:\\copy2.jpg", true);
            byte[] bytes = new byte[256];
            int i;
            while ((i = fileInputStream.read(bytes))> 0){
                fileOutputStream.write(bytes);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void test3(String path){
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            fileOutputStream = new FileOutputStream("e:\\copy3.jpg", true);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            byte[] bytes = new byte[256];
            int i;
            while ((i = fileInputStream.read(bytes)) > 0){
                bufferedOutputStream.write(bytes);
                bufferedOutputStream.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        IOtest iOtest = new IOtest();
        long starttime = System.currentTimeMillis();
        iOtest.test("d:\\compress.jpg");
        long endtime = System.currentTimeMillis();
        long time = endtime -starttime;
        System.out.println("fileoutputstream写入共耗时:" + time);


        long starttime1 = System.currentTimeMillis();
        iOtest.test2("d:\\compress1.jpg");
        long endtime1 = System.currentTimeMillis();
        long time1 = endtime1 -starttime1;
        System.out.println("buffered写入共耗时:" + time1);


        long starttime2 = System.currentTimeMillis();
        iOtest.test3("d:\\compress2.jpg");
        long endtime2 = System.currentTimeMillis();
        long time2 = endtime2 -starttime2;
        System.out.println("bufferedOutputStream写入共耗时:" + time2);
    }
}

结果如下:
fileoutputstream 写入共耗时:7742
buffered 写入共耗时:41
bufferedOutputStream 写入共耗时:57
bufferedOutputStream 的耗时跟 buffered 的使用方法几乎一样

所以我们平时在使用 fileoutputStream 流时记得使用 buffer 的方式进行读入和写出,这种效率较高,并不一定要使用 bufferedOutputStream

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册