小测试
使用缓冲流是否一定可以提高复制效率呢,下面能过一个小测试便可知道答案。
此小测试的内容是使用代理的知识测出不使用缓冲流与使用缓冲流时复制一个文件所需要的时间。
因为涉及到代理,所以这里我用到了3个类与2个接口:- import java.io.*;
- import java.lang.reflect.*;
- //1. MyTest 目标类
- public class MyTest implements TheImplemetedInterfaceOfMyTest
- {
- public static void main(String[] args)
- {
- File sourceVideo = new File("d:/e.3gp");
- File targetVideo = new File("d:/f.3gp");
- File targetVideo1 = new File("d:/g.3gp");
- final MyTest target = new MyTest();
- final MyAdvice advice = new MyAdvice();
- TheImplemetedInterfaceOfMyTest test = (TheImplemetedInterfaceOfMyTest) new MyProxy().getProxy(target, advice);
- test.copy(sourceVideo, targetVideo);
- test.copy1(sourceVideo, targetVideo1);
- }
-
- public boolean copy(File sourceFile, File targetFile) {
-
- byte [] buf = new byte[1024];
-
- OutputStream os = null;
- InputStream is =null;
- DataOutputStream dos = null;
- DataInputStream dis = null;
- try {
- is = new FileInputStream(sourceFile);
- os = new FileOutputStream(targetFile);
- dis = new DataInputStream(is);
- dos = new DataOutputStream(os);
- int len = dis.read(buf);
- while(len != -1){
- dos.write(buf,0,len);
- len = dis.read(buf);
- }
- return true;
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- return false;
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- }
- public boolean copy1(File sourceFile, File targetFile) {
-
- byte [] buf = new byte[1024];
-
- OutputStream os = null;
- InputStream is =null;
- BufferedInputStream bis = null;
- BufferedOutputStream bos = null;
- DataOutputStream dos = null;
- DataInputStream dis = null;
- try {
- is = new FileInputStream(sourceFile);
- bis = new BufferedInputStream(is);
- dis = new DataInputStream(bis);
- os = new FileOutputStream(targetFile);
- bos = new BufferedOutputStream(os);
- dos = new DataOutputStream(bos);
- int len = dis.read(buf);
- while(len != -1){
- dos.write(buf,0,len);
- len = dis.read(buf);
- }
- return true;
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- return false;
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- }
-
- }
- //2. MyProxy 代理类 通过我的代理获得一个代理对象
- class MyProxy {
- public Object getProxy(final Object target,final Advice advice) {
- Object proxy = Proxy.newProxyInstance(
- target.getClass().getClassLoader(),
- target.getClass().getInterfaces(),
- new InvocationHandler(){
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- advice.beforeMethod();
- Object retVal = method.invoke(target, args);
- advice.afterMethod();
- return retVal;
- }});
- return proxy;
- }
-
- }
- //3. MyAdvice 计算方法运行时间的类
- class MyAdvice implements Advice {
- long startTime = 0;
- public void afterMethod() {
- // TODO Auto-generated method stub
- long endTime = System.currentTimeMillis();
- System.out.println(endTime - startTime);
- }
- public void beforeMethod() {
- // TODO Auto-generated method stub
- startTime = System.currentTimeMillis();
- }
- }
- //4. TheImplemetedInterfaceOfMyTest 目标类所实现的接口
- interface TheImplemetedInterfaceOfMyTest {
- boolean copy(File sourceFile, File targetFile);
- boolean copy1(File sourceFile, File targetFile);
- }
- //5. Advice 代理忠告接口
- interface Advice {
- void beforeMethod();
- void afterMethod();
- }
复制代码 测试结果:
(具体测试结果自己测试观察便可知道)
测试结论:
当缓存buf为1024字节,文件大小为2M左右,即缓存buf比文件小时,使用了缓冲流比不使用快;
当缓存buf为1024*1024*10字节,文件大小为2M左右,即缓存buf比文件大时,没有使用缓冲流的变快了,
使得两者复制的时间差不多,但还是使用了缓冲流的稍快一点;两者都比使用缓存buf为10字节时快。
最终结论:
若要提提高复制效率,应尽可能使用缓冲流。
如果不使用缓冲流,则应把缓存buf尽量设到与要复制的文件一样大小或稍大。
使用缓冲流时,也应把缓存buf尽量设到与要复制的文件一样大小或稍大。 |
|