A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张水荣 中级黑马   /  2012-7-21 14:20  /  1276 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

小测试
使用缓冲流是否一定可以提高复制效率呢,下面能过一个小测试便可知道答案。
此小测试的内容是使用代理的知识测出不使用缓冲流与使用缓冲流时复制一个文件所需要的时间。
因为涉及到代理,所以这里我用到了3个类与2个接口:
  1. import java.io.*;

  2. import java.lang.reflect.*;

  3. //1. MyTest 目标类
  4. public class MyTest  implements TheImplemetedInterfaceOfMyTest
  5. {
  6.         public static void main(String[] args)
  7.         {
  8.                 File sourceVideo = new File("d:/e.3gp");
  9.                 File targetVideo = new File("d:/f.3gp");
  10.                 File targetVideo1 = new File("d:/g.3gp");
  11.                 final MyTest target = new MyTest();
  12.                 final MyAdvice advice = new MyAdvice();
  13.                 TheImplemetedInterfaceOfMyTest test = (TheImplemetedInterfaceOfMyTest) new MyProxy().getProxy(target, advice);
  14.                 test.copy(sourceVideo, targetVideo);
  15.                 test.copy1(sourceVideo, targetVideo1);
  16.         }
  17.         
  18.         public   boolean copy(File sourceFile, File targetFile) {
  19.                
  20.                     byte [] buf = new byte[1024];
  21.                     
  22.                      OutputStream os = null;
  23.                      InputStream is =null;
  24.                      DataOutputStream dos = null;
  25.                      DataInputStream dis = null;
  26.                     try {
  27.                             is = new FileInputStream(sourceFile);
  28.                             os = new FileOutputStream(targetFile);
  29.                             dis = new DataInputStream(is);
  30.                             dos = new DataOutputStream(os);
  31.                             int len = dis.read(buf);
  32.                             while(len != -1){
  33.                                     dos.write(buf,0,len);
  34.                                     len = dis.read(buf);
  35.                             }
  36.                             return true;
  37.                     } catch (FileNotFoundException e) {
  38.                             e.printStackTrace();
  39.                             return false;
  40.                     } catch (IOException e) {
  41.                             e.printStackTrace();
  42.                             return false;
  43.                     }
  44.             }

  45.         public   boolean copy1(File sourceFile, File targetFile) {
  46.                
  47.                     byte [] buf = new byte[1024];
  48.                     
  49.                      OutputStream os = null;
  50.                      InputStream is =null;
  51.                      BufferedInputStream bis = null;
  52.                      BufferedOutputStream bos = null;
  53.                      DataOutputStream dos = null;
  54.                      DataInputStream dis = null;
  55.                     try {
  56.                             is = new FileInputStream(sourceFile);
  57.                             bis = new BufferedInputStream(is);
  58.                             dis = new DataInputStream(bis);
  59.                             os = new FileOutputStream(targetFile);
  60.                             bos = new BufferedOutputStream(os);
  61.                             dos = new DataOutputStream(bos);
  62.                             int len = dis.read(buf);
  63.                             while(len != -1){
  64.                                     dos.write(buf,0,len);
  65.                                     len = dis.read(buf);
  66.                             }
  67.                             return true;
  68.                     } catch (FileNotFoundException e) {
  69.                             e.printStackTrace();
  70.                             return false;
  71.                     } catch (IOException e) {
  72.                             e.printStackTrace();
  73.                             return false;
  74.                     }
  75.             }
  76.             
  77. }

  78. //2. MyProxy 代理类 通过我的代理获得一个代理对象
  79. class MyProxy {

  80.         public Object getProxy(final Object target,final Advice advice) {
  81.                 Object proxy = Proxy.newProxyInstance(
  82.                                 target.getClass().getClassLoader(),
  83.                                 target.getClass().getInterfaces(),
  84.                                 new InvocationHandler(){
  85.                         public Object invoke(Object proxy, Method method, Object[] args)
  86.                                         throws Throwable {
  87.                                 advice.beforeMethod();
  88.                                 Object retVal = method.invoke(target, args);
  89.                                 advice.afterMethod();
  90.                                 return retVal;
  91.                         }});
  92.                 return proxy;
  93.         }
  94.         
  95. }

  96. //3. MyAdvice 计算方法运行时间的类
  97. class MyAdvice implements Advice {
  98.         long startTime = 0;
  99.         public void afterMethod() {
  100.                 // TODO Auto-generated method stub
  101.                 long endTime = System.currentTimeMillis();
  102.                 System.out.println(endTime - startTime);
  103.         }

  104.         public void beforeMethod() {
  105.                 // TODO Auto-generated method stub
  106.                 startTime = System.currentTimeMillis();
  107.         }

  108. }

  109. //4. TheImplemetedInterfaceOfMyTest 目标类所实现的接口
  110. interface TheImplemetedInterfaceOfMyTest {
  111.         boolean copy(File sourceFile, File targetFile);
  112.         boolean copy1(File sourceFile, File targetFile);
  113. }

  114. //5. Advice 代理忠告接口
  115. interface Advice {
  116.         void beforeMethod();
  117.         void afterMethod();
  118. }
复制代码
测试结果:
  (具体测试结果自己测试观察便可知道)

测试结论:
  当缓存buf为1024字节,文件大小为2M左右,即缓存buf比文件小时,使用了缓冲流比不使用快;
  当缓存buf为1024*1024*10字节,文件大小为2M左右,即缓存buf比文件大时,没有使用缓冲流的变快了,
       使得两者复制的时间差不多,但还是使用了缓冲流的稍快一点;两者都比使用缓存buf为10字节时快。
  
最终结论:
  若要提提高复制效率,应尽可能使用缓冲流。
  如果不使用缓冲流,则应把缓存buf尽量设到与要复制的文件一样大小或稍大。
  使用缓冲流时,也应把缓存buf尽量设到与要复制的文件一样大小或稍大。

2 个回复

倒序浏览
如果我要拷贝高清电影(1G),应该使用多少缓存才好呢?不可能开一个跟这个电影差不多大的缓存。。。。。
回复 使用道具 举报
张水荣 来自手机 中级黑马 2012-7-21 15:56:15
藤椅
韦念欣 发表于 2012-7-21 14:46 如果我要拷贝高清电影(1G),应该使用多少缓存才好呢?不可能开一个跟这个电影差不多大的缓存。。。。。 ...

我那结论只是相对来说的。比较适合于十来几十兆的文件!估计缓存开太大复制效率也未心会太高,应该还要看硬盘的读写速度吧。设成与硬盘的读写速度那个值应该是效率最高的吧!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马