本帖最后由 秦斌 于 2013-1-3 10:51 编辑
准备文件jdk-7u9-windows-x64.exe (94,398,944字节)
代码1,不用Buffered:- import java.io.*;
- class CopyTest{
- public static void main(String[] args)throws IOException{
- long start = System.currentTimeMillis();
- FileInputStream fis = new FileInputStream("D:\\jdk-7u9-windows-x64.exe");
- FileOutputStream fos = new FileOutputStream("D:\\jdk-7u9-windows-x64-2.exe");
- //可将下面的byte数组该为1024或者1024*10或者1024*1024*10
- byte[] b = new byte[1024*1024];
- int len;
- while ((len = fis.read(b))!=-1){
- fos.write(b,0,len);
- }
- fis.close();
- fos.close();
- long end = System.currentTimeMillis();
- System.out.println((end-start)+"毫秒");
- }
- }
复制代码 代码2,使用buffered:- import java.io.*;
- class BufferedCopyTest{
- public static void main(String[] args)throws IOException{
- long start = System.currentTimeMillis();
- //可使用另一个构造函数设置缓冲大小
- //可将下语句中的1024*1024改为1024或1024*10或1024*1024*10
- //BufferedInputStream buffis = new BufferedInputStream (new FileInputStream("D:\\jdk-7u9-windows-x64.exe"),1024*1024);
- BufferedInputStream buffis = new BufferedInputStream (new FileInputStream("D:\\jdk-7u9-windows-x64.exe"));
- //可将下语句中的1024*1024改为1024或1024*10或1024*1024*10
- //BufferedOutputStream buffos = new BufferedOutputStream(new FileOutputStream("D:\\jdk-7u9-windows-x64-2.exe"));
- BufferedOutputStream buffos = new BufferedOutputStream(new FileOutputStream("D:\\jdk-7u9-windows-x64-2.exe"));
- int num;
- while ((num = buffis.read())!=-1){
- buffos.write(num);
- }
- buffis.close();
- buffos.close();
- long end = System.currentTimeMillis();
- System.out.println((end-start)+"毫秒");
- }
- }
复制代码 代码1,居然只用时0.2秒左右。
代码2,大概需要不到4秒的时间。
问题来了:
1、带Buffered的居然还慢些,这是为啥?
2、代码1,居然超出了硬盘的物理极限,这是为啥?
|