why i copy files faster than you

IO stream copy file speed comparison

Let's take a copy of the video as an example


IO flow classification

Ready to work

  • Timing method ideas
  • System.currentTimeMillis()Returns the current time in milliseconds
long start = System.currentTimeMillis();
//调用复制方法
emthod01();
long end = System.currentTimeMillis();
System.out.println("共耗时" + (end - start) + "毫秒");
  • video size used

The first to play is FileOutputStreamandFileInputStream

  • We first use the method of reading one byte at a time
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();
}
  • result

It took a total of 140475 milliseconds

It took more than two minutes, I thought I wrote it wrong

  • Then we read one byte array at a time
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();
}
  • result

It took a total of 211 milliseconds


Then came BufferedOutputStreamtheBufferedInputStream

  • Let's try reading one byte at a time
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();
}
  • result

It took a total of 349 milliseconds

  • Then read one byte array at a time
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();
}
  • result

It took a total of 43 milliseconds

Then we copy the text to compare

  • We first generate a text file
 //生成一个文件
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();
}
  • The file properties are as follows

1: FileInputStreamJapaneseFileOutputStream

  • one character at a time
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();
}
  • Result (estimated to be the slowest again)

It took a total of 16273 milliseconds

  • one byte array at a time
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();
    }
  • result

Takes a total of 31 milliseconds

Two: BufferedInputStreamJapaneseBufferedOutputStream

  • one character at a time
    • First close the outer flow bossum bis, then close the inner laminar flow fissumfos
    • When the outer flow is closed, the inner flow closes itself, so only the outer flow needs to be closed.
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();
}

  • result
  • The reason for this result is

It took a total of 16273 milliseconds

  • one byte array at a time
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();
}

  • result

Takes a total of 28 milliseconds

3: FileReaderJapaneseFileWriter

  • Note: FileReaderand FileWritercan only handle character streams

  • one character at a time

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

It took a total of 122 milliseconds

  • one character array at a time
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();
    }
  • result

Takes a total of 66 milliseconds

Four: BufferedReaderandBufferedWriter

BufferedReaderIn addition to inheriting the methods of the Reader class, it has its own unique method:
String readLine(): Read a line of data, termination symbols:
line feed ( \n), carriage return ( \r), \r\n(windows)
Its return value contains the string of the new line, not including any terminator, or null if the end of the stream is reached

  • one character at a time
 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();
    }
  • result

Takes a total of 80 milliseconds

  • one character array at a time
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();

}
  • result

Takes a total of 49 milliseconds

  • unique method
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();
}
  • result

Takes a total of 74 milliseconds

See here, click on the attention and then go

Guess you like

Origin blog.csdn.net/m0_57025749/article/details/123952074