¿Por qué copio archivos más rápido que tú?

Comparación de velocidad de archivo de copia de flujo de IO

Tomemos una copia del video como ejemplo.


Clasificación de flujo de E/S

Listo para trabajar

  • Ideas de métodos de tiempo
  • System.currentTimeMillis()Devuelve la hora actual en milisegundos
long start = System.currentTimeMillis();
//调用复制方法
emthod01();
long end = System.currentTimeMillis();
System.out.println("共耗时" + (end - start) + "毫秒");
  • tamaño de vídeo utilizado

El primero en jugar es FileOutputStreamyFileInputStream

  • Primero usamos el método de leer un byte a la vez
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();
}
  • resultado

Tomó un total de 140475 milisegundos

Tardó más de dos minutos, pensé que lo había escrito mal.

  • Luego leemos una matriz de bytes a la vez
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();
}
  • resultado

Tomó un total de 211 milisegundos.


Luego vino BufferedOutputStreamelBufferedInputStream

  • Intentemos leer un byte a la vez
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();
}
  • resultado

Tomó un total de 349 milisegundos.

  • Luego lea una matriz de bytes a la vez
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();
}
  • resultado

Tomó un total de 43 milisegundos.

Luego copiamos el texto para comparar

  • Primero generamos un archivo de texto
 //生成一个文件
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();
}
  • Las propiedades del archivo son las siguientes

1: FileInputStreamjaponésFileOutputStream

  • un personaje a la vez
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();
}
  • Resultado (estimado como el más lento de nuevo)

Tomó un total de 16273 milisegundos

  • una matriz de bytes a la vez
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();
    }
  • resultado

Toma un total de 31 milisegundos

dos: BufferedInputStreamjaponésBufferedOutputStream

  • un personaje a la vez
    • bosPrimero cierre la suma de flujo externo , luego cierre la suma bisde flujo laminar internofisfos
    • Cuando el flujo externo está cerrado, el flujo interno se cierra solo, por lo que solo es necesario cerrar el flujo externo.
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();
}

  • resultado
  • La razón de este resultado es

Tomó un total de 16273 milisegundos

  • una matriz de bytes a la vez
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();
}

  • resultado

Toma un total de 28 milisegundos

3: FileReaderjaponésFileWriter

  • Nota: FileReadery FileWritersolo puede manejar secuencias de caracteres

  • un personaje a la vez

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();
}
  • resultado

Tomó un total de 122 milisegundos.

  • una matriz de caracteres a la vez
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();
    }
  • resultado

Toma un total de 66 milisegundos

Cuatro: BufferedReaderyBufferedWriter

BufferedReaderAdemás de heredar los métodos de la clase Reader, tiene su propio método único:
String readLine(): Leer una línea de datos, símbolos de terminación:
avance de línea ( \n), retorno de carro ( \r), \r\n(ventanas)
Su valor de retorno contiene la cadena del nuevo línea, sin incluir ningún terminador, o nulo si se llega al final de la secuencia

  • un personaje a la vez
 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();
    }
  • resultado

Toma un total de 80 milisegundos

  • una matriz de caracteres a la vez
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();

}
  • resultado

Toma un total de 49 milisegundos

  • método único
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();
}
  • resultado

Toma un total de 74 milisegundos

Vea aquí, haga clic en la atención y luego vaya

Supongo que te gusta

Origin blog.csdn.net/m0_57025749/article/details/123952074
Recomendado
Clasificación