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

© 刘晓康 中级黑马   /  2012-4-1 13:50  /  1673 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

对于缓冲字节流的疑问:虽然进行了缓冲但是如果像上面的这样取,不是还是很慢吗,虽然是从缓冲里面取还是一个一个的取,效率还是很慢,然后做了个实验我使用一次取1024个,结果效率提高20倍,那么我就想,不用缓冲直接就用字节数组去读写,是不是也比缓冲要快呢,求解?
下面是我做的实验代码:

import java.io.*;
class CopyMp3
{
public static void main(String[] args)
{
  long start = System.currentTimeMillis();
  copy();
  long end = System.currentTimeMillis();
  System.out.println("time="+ (end-start)+"毫秒");

  
  
  

  
}

public static void copy()
{
  BufferedInputStream bis = null;
  BufferedOutputStream bos = null;
  try
  {
   bis = new BufferedInputStream(new FileInputStream("F:\\duqing.mp3"));
      bos = new BufferedOutputStream(new FileOutputStream("F:\\渡情.mp3"));
   byte[] bt = new byte[1024];
   int buf = 0;
   while((buf = bis.read(bt))!= -1)
   {
    bos.write(bt, 0, buf);
   }
  }
  catch (IOException e)
  {
   throw new RuntimeException("复制失败!");
  }
  finally
  {
   try
   {
    if(bis!=null)
     bis.close();
   }
   catch (IOException e)
   {
    throw new RuntimeException("读文件关闭失败");
   }
   try
   {
    if(bos!=null)
     bos.close();
   }
   catch (IOException e)
   {
    throw new RuntimeException("写文件关闭失败");
   }
   
  }
}
}

3 个回复

正序浏览
效率 在目前的情况下 根本看不出有什么区别    除非大数据量下
回复 使用道具 举报
呃。。。视频看到哪儿了?
回复 使用道具 举报
老大  这个就是算法问题了  一次性读取肯定比 一个 一个的去读速度快的   你上面说的2个完全没有可比性啊 你应该一个没有用缓存 一个用了缓存 相同的算法去比较 才有可比性  不过 如果你读取的数据非常小的话   我估计你用了缓存反而影响效率
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马