黑马程序员技术交流社区

标题: 对于缓冲字节流的疑问 [打印本页]

作者: 薛波    时间: 2012-3-26 15:20
标题: 对于缓冲字节流的疑问
对于缓冲字节流的疑问:虽然进行了缓冲但是如果像上面的这样取,不是还是很慢吗,虽然是从缓冲里面取还是一个一个的取,效率还是很慢,然后做了个实验我使用一次取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("写文件关闭失败");
   }
   
  }
}
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2