http://www.iteye.com/topic/409525
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package test; 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毫秒
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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毫秒
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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毫秒