缓冲流的效率测试_复制文件

缓冲流的效率测试_复制文件

文件复制练习:一读一写

明确:

  数据源:c:\\1.jpg

  数据的目的地:d:\\1.jpg

文件复制的步骤:

  1、创建字节缓冲输入流对象,构造方法中传递字节输入流

  2、创建字节缓冲输出流对象,构造方法中传递字节输出流

  3、使用字节缓冲输入流对象中的方法read,读取文件

  4、使用字节缓冲输出流对象中的方法write,把读取的数据写入到内部缓冲区中

  5、释放资源(会先把缓冲区中的数据,刷新到文件中)

public class DemoBuffered_02 {
    public static void main(String[] args) throws IOException {

        long l = System.currentTimeMillis();//获取毫秒

        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("E:\\a.png"));
        
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("E:\\m.png"));

        byte[] bys = new byte[1024];
        int len = 0;
        while ((len=bufferedInputStream.read(bys))!= -1){
            bufferedOutputStream.write(len);
        }

        long l1 = System.currentTimeMillis();
        System.out.println("复制文件共消耗:"+(l1-l));

    }
}

运行结果:

 

posted @ 2022-07-20 13:45  monkey大佬  阅读(27)  评论(0)    收藏  举报