FileInputStream,FileOutputStream文件拷贝之性能比较

package com.kingsoft.main;

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

public class TestIO03 {
  
  /**
   * @param args
   * @throws IOException
   */
  public static void main(String[] args) throws IOException {
    
    InputStream ins = new FileInputStream(
        "E:\\tools\\FreeMind-Windows-Installer-0.8.1-max.exe");
    OutputStream ous = new FileOutputStream(
        "D:\\FreeMind-Windows-Installer-0.8.1-max.exe_bk");
    
    byte[] contents = new byte[ins.available()];
    
    long start = System.currentTimeMillis();
    // 读取文件
    int i = 0;
    int count = 0;
    while ((i = ins.read()) != -1) {
      contents[count] = (byte) i;
      count++;
    }
    
    long end = System.currentTimeMillis();
    System.out.println("读取耗时:" + (end - start) + "毫秒");
    
    start = System.currentTimeMillis();
    // 输出文件
    for (int j = 0; j < contents.length; j++) {
      ous.write(contents[j]);
    }
    
    end = System.currentTimeMillis();
    System.out.println("读取耗时:" + (end - start) + "毫秒");
    
    ins.close();
    ous.flush();
    ous.close();
  }
}

执行结果如下:
读取耗时:13329毫秒
读取耗时:16083毫秒

    InputStream ins = new FileInputStream(
        "E:\\tools\\FreeMind-Windows-Installer-0.8.1-max.exe");
    OutputStream ous = new FileOutputStream(
        "D:\\FreeMind-Windows-Installer-0.8.1-max.exe_bk");
    
    long start = System.currentTimeMillis();
    int i = 0;
    while ((i = ins.read()) != -1) {
      ous.write(i);
    }
    
    long end = System.currentTimeMillis();
    System.out.println("耗时:" + (end - start) + "毫秒");
    
    ins.close();
    ous.flush();
    ous.close();

执行结果如下:
耗时:30013毫秒

    InputStream ins = new FileInputStream(
        "E:\\tools\\FreeMind-Windows-Installer-0.8.1-max.exe");
    OutputStream ous = new FileOutputStream(
        "D:\\FreeMind-Windows-Installer-0.8.1-max.exe_bk");
    
    byte[] contents = new byte[ins.available()];
    
    long start = System.currentTimeMillis();
    ins.read(contents);
    ous.write(contents);
    
    long end = System.currentTimeMillis();
    System.out.println("耗时:" + (end - start) + "毫秒");
    
    ins.close();
    ous.flush();
    ous.close();

执行结果如下:
耗时:2384毫秒

你可能感兴趣的:(java,windows,J#)