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

5次阅读

共计 2041 个字符,预计需要花费 6 分钟才能阅读完成。

package com.itheima.demo02.CopyFile;

import java.io.*;

/*

 文件复制练习: 一读一写

明确:
    数据源: c:\\1.jpg
    数据的目的地: d:\\1.jpg
文件复制的步骤:
    1. 创建字节缓冲输入流对象, 构造方法中传递字节输入流
    2. 创建字节缓冲输出流对象, 构造方法中传递字节输出流
    3. 使用字节缓冲输入流对象中的方法 read, 读取文件
    4. 使用字节缓冲输出流中的方法 write, 把读取的数据写入到内部缓冲区中
    5. 释放资源 (会先把缓冲区中的数据, 刷新到文件中)

文件的大小:780,831 字节
一次读写一个字节:32 毫秒
使用数组缓冲读取多个字节, 写入多个字节:5 毫秒 

*/
public class Demo02CopyFile {

public static void main(String[] args) throws IOException {long s = System.currentTimeMillis();
    //1. 创建字节缓冲输入流对象, 构造方法中传递字节输入流
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("c:\\1.jpg"));
    //2. 创建字节缓冲输出流对象, 构造方法中传递字节输出流
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:\\1.jpg"));
    //3. 使用字节缓冲输入流对象中的方法 read, 读取文件
    // 一次读取一个字节写入一个字节的方式
    /*int len = 0;
    while((len = bis.read())!=-1){bos.write(len);
    }*/

    // 使用数组缓冲读取多个字节, 写入多个字节
    byte[] bytes = new byte[1024];
    int len = 0;
    while((len = bis.read(bytes))!=-1){bos.write(bytes,0,len);
    }

    bos.close();
    bis.close();

    long e = System.currentTimeMillis();
    System.out.println("复制文件共耗时:"+(e-s)+"毫秒");
}

}
不实用缓冲流的方式
package com.itheima.demo02.CopyFile;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*

 文件复制练习: 一读一写

明确:
    数据源: c:\\1.jpg
    数据的目的地: d:\\1.jpg

文件复制的步骤:
    1. 创建一个字节输入流对象, 构造方法中绑定要读取的数据源
    2. 创建一个字节输出流对象, 构造方法中绑定要写入的目的地
    3. 使用字节输入流对象中的方法 read 读取文件
    4. 使用字节输出流中的方法 write, 把读取到的字节写入到目的地的文件中
    5. 释放资源
文件的大小:780,831 字节
一次读写一个字节:6043 毫秒
使用数组缓冲读取多个字节, 写入多个字节:10 毫秒 

*/
public class Demo01CopyFile {

public static void main(String[] args) throws IOException {long s = System.currentTimeMillis();
    //1. 创建一个字节输入流对象, 构造方法中绑定要读取的数据源
    FileInputStream fis = new FileInputStream("c:\\1.jpg");
    //2. 创建一个字节输出流对象, 构造方法中绑定要写入的目的地
    FileOutputStream fos = new FileOutputStream("d:\\1.jpg");
    // 一次读取一个字节写入一个字节的方式
    //3. 使用字节输入流对象中的方法 read 读取文件
    /*int len = 0;
    while((len = fis.read())!=-1){
        //4. 使用字节输出流中的方法 write, 把读取到的字节写入到目的地的文件中
        fos.write(len);
    }*/

    // 使用数组缓冲读取多个字节, 写入多个字节
    byte[] bytes = new byte[1024];
    //3. 使用字节输入流对象中的方法 read 读取文件
    int len = 0;// 每次读取的有效字节个数
    while((len = fis.read(bytes))!=-1){
        //4. 使用字节输出流中的方法 write, 把读取到的字节写入到目的地的文件中
        fos.write(bytes,0,len);
    }

    //5. 释放资源 (先关写的, 后关闭读的; 如果写完了, 肯定读取完毕了)
    fos.close();
    fis.close();
    long e = System.currentTimeMillis();
    System.out.println("复制文件共耗时:"+(e-s)+"毫秒");
}

}

正文完
 0