为什么我复制文件比你快

IO流复制文件速度对比

我们先以复制视频为例


IO流分类

准备工作

  • 计时方法思路
  • System.currentTimeMillis()以毫秒为单位返回当前时间
long start = System.currentTimeMillis();
//调用复制方法
emthod01();
long end = System.currentTimeMillis();
System.out.println("共耗时" + (end - start) + "毫秒");
  • 使用的视频大小

首先上场的是FileOutputStreamFileInputStream

  • 我们先用一次读取一个字节的方法
public static void emthod01() throws IOException {
    
    
    FileOutputStream fos = new FileOutputStream("D:\\3022809742\\王者荣耀年度CG2.mp4");
    FileInputStream fis = new FileInputStream("D:\\3022809742\\王者荣耀年度CG.mp4");
    int by;
    while ((by = fis.read()) != -1) {
    
    
        fos.write(by);
    }
    fos.close();
    fis.close();
}
  • 结果

共耗时140475毫秒

竟然用了两分多钟,我一度以为我写错了

  • 接着我们一次读取一个字节数组
public static void emthod02() throws IOException {
    
    
    FileOutputStream fos = new FileOutputStream("D:\\3022809742\\王者荣耀年度CG2.mp4");
    FileInputStream fis = new FileInputStream("D:\\3022809742\\王者荣耀年度CG.mp4");
    byte[] bytes = new byte[1024];
    int len;
    while ((len = fis.read(bytes)) != -1) {
    
    
        fos.write(bytes, 0, len);
    }
    fos.close();
    fis.close();
}
  • 结果

共耗时211毫秒


接着上场的是BufferedOutputStreamBufferedInputStream

  • 我们还是先试一下一次读取一个字节
public static void emthod03() throws IOException {
    
    
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\3022809742\\王者荣耀年度CG2.mp4"));
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\3022809742\\王者荣耀年度CG.mp4"));
    int by;
    while ((by = bis.read()) != -1) {
    
    
        bos.write(by);
    }
    bos.close();
    bis.close();
}
  • 结果

共耗时349毫秒

  • 接着用一次读取一个字节数组的方式
public static void emthod04() throws IOException {
    
    
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\3022809742\\王者荣耀年度CG2.mp4"));
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\3022809742\\王者荣耀年度CG.mp4"));
    byte[] bytes = new byte[1024];
    int len;
    while ((len = bis.read(bytes)) != -1) {
    
    
        bos.write(bytes, 0, len);
    }
    bos.close();
    bis.close();
}
  • 结果

共耗时43毫秒

接着我们用复制文本来比较

  • 我们先生成一个文本文件
 //生成一个文件
public static void emthod() throws IOException {
    
    
    FileWriter fw = new FileWriter(new File("D:\\3022809742\\zjw.txt"));
    int i = 0;
    while (i < 1000000) {
    
    
        fw.write("好");
        i++;
    }
    fw.close();
}
  • 文件属性如下

一 :FileInputStreamFileOutputStream

  • 一次一个字符
public static void emthod1() throws IOException {
    
    

    FileInputStream fis = new FileInputStream("D:\\3022809742\\zjw.txt");
    FileOutputStream fos = new FileOutputStream("D:\\3022809742\\zjw2.txt");
    int by;
    while ((by = fis.read()) != -1) {
    
    
        fos.write(by);
    }
    fos.close();
    fis.close();
}
  • 结果(估计又是最慢的的)

共耗时16273毫秒

  • 一次一个字节数组
public static void emthod2() throws IOException {
    
    

        FileInputStream fis = new FileInputStream("D:\\3022809742\\zjw.txt");
        FileOutputStream fos = new FileOutputStream("D:\\3022809742\\zjw2.txt");
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = fis.read(bytes)) != -1) {
    
    
            fos.write(bytes, 0, len);
        }
        fos.close();

        fis.close();
    }
  • 结果

共耗时31毫秒

二 :BufferedInputStreamBufferedOutputStream

  • 一次一个字符
    • 先关闭外层流bosbis,再关闭内层流fisfos
    • 关闭外层流时, 内层流自行关闭,所以只需要关闭外层流。
public static void emthod3() throws IOException {
    
    

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("D:\\3022809742\\zjw.txt")));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\3022809742\\zjw2.txt")));

    int by;
    while ((by = bis.read()) != -1) {
    
    
        bos.write(by);
        bos.flush();
    }

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

  • 结果
  • 产生这个结果的原因是

共耗时16273毫秒

  • 一次一个字节数组
public static void emthod4() throws IOException {
    
    

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("D:\\3022809742\\zjw.txt")));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\3022809742\\zjw2.txt")));

    int len = 0;
    byte[] bytes = new byte[1024];
    while ((len = bis.read(bytes)) != -1) {
    
    
        bos.write(bytes, 0, len);
        bos.flush();
    }
    bis.close();
    bos.close();
}

  • 结果

共耗时28毫秒

三 : FileReaderFileWriter

  • 注意:FileReaderFileWriter只能处理字符流

  • 一次一个字符

public static void emthod5() throws IOException {
    
    
    FileReader fr = new FileReader(new File("D:\\3022809742\\zjw.txt"));
    FileWriter fw = new FileWriter(new File("D:\\3022809742\\zjw2.txt"));
    int by;
    while ((by = fr.read()) != -1) {
    
    
        fw.write(by);
    }
    fw.close();
    fr.close();
}
  • 结果

共耗时122毫秒

  • 一次一个字符数组
public static void emthod6() throws IOException {
    
    
        FileReader fr = new FileReader(new File("D:\\3022809742\\zjw.txt"));
        FileWriter fw = new FileWriter(new File("D:\\3022809742\\zjw2.txt"));
        int len = 0;
        char[] chars = new char[1024];
        while ((len = fr.read(chars)) != -1) {
    
    
            fw.write(chars, 0, len);
        }
        fw.close();
        fr.close();
    }
  • 结果

共耗时66毫秒

四 :BufferedReaderBufferedWriter

BufferedReader除了继承了Reader类的方法之外,有自己的独有方法:
String readLine():读取一行数据,终止符号:
换行(\n),回车(\r),\r\n(windows)
它的返回值包含改行的字符串,不包含任何终止符号,如果到达流末尾,返回null

  • 一次一个字符
 public static void emthod7() throws IOException {
    
    

        BufferedReader br = new BufferedReader(new FileReader(new File("D:\\3022809742\\zjw.txt")));
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("D:\\3022809742\\zjw2.txt")));

        int by;
        while ((by = br.read()) != -1) {
    
    
            bw.write(by);
        }
        //释放资源
        br.close();
        bw.close();
    }
  • 结果

共耗时80毫秒

  • 一次一个字符数组
public static void emthod8() throws IOException {
    
    

    BufferedReader br = new BufferedReader(new FileReader(new File("D:\\3022809742\\zjw.txt")));
    BufferedWriter bw = new BufferedWriter(new FileWriter(new File("D:\\3022809742\\zjw2.txt")));

    char[] chars = new char[1024];
    int len;
    while ((len = br.read(chars)) != -1) {
    
    
        new String(chars, 0, len);
    }
    //释放资源
    br.close();
    bw.close();

}
  • 结果

共耗时49毫秒

  • 特有方法
public static void emthod9() throws IOException {
    
    

    BufferedReader br = new BufferedReader(new FileReader(new File("D:\\3022809742\\zjw.txt")));
    BufferedWriter bw = new BufferedWriter(new FileWriter(new File("D:\\3022809742\\zjw2.txt")));

    String data;
    while ((data = br.readLine()) != null) {
    
    
        bw.write(data);
        // 换行
        bw.newLine();
    }
    //释放资源
    br.close();
    bw.close();
}
  • 结果

共耗时74毫秒

看到这里了,点个关注再走吧

猜你喜欢

转载自blog.csdn.net/m0_57025749/article/details/123952074
今日推荐