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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© as604049322 金牌黑马   /  2014-12-3 10:30  /  1603 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 as604049322 于 2014-12-3 10:39 编辑
  1. import java.io.*;
  2. class  CopyMp3
  3. {
  4.         public static void main(String[] args) throws IOException
  5.         {
  6.                 long start = System.currentTimeMillis();
  7.                 copy_2();
  8.                 long end = System.currentTimeMillis();
  9.                 System.out.println("copy2消耗时间"+(end-start)+"毫秒");
  10.         }
  11.         
  12.         public static void copy_3()throws IOException
  13.         {
  14.                 FileInputStream bufis = new FileInputStream("d:\\in.mp3");
  15.                 BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("d:\\out3.mp3"));
  16.                
  17.                 int num  = 0;
  18.                 byte[] buf = new byte[1024*4];
  19.                 //System.out.println("第一个字节:"+bufis.myRead());

  20.                 while((num=bufis.read(buf))!=-1)
  21.                 {
  22.                         for(byte i:buf)
  23.                                 bufos.write(i);
  24.                         //for(int i=0;i<num;i++)
  25.                                 //bufos.write(buf[i]);
  26.                 }

  27.                 bufos.close();
  28.                 bufis.close();
  29.         }

  30.         public static void copy_2()throws IOException
  31.         {
  32.                 MyBufferedInputStream bufis = new MyBufferedInputStream(new FileInputStream("d:\\in.mp3"));
  33.                 BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("d:\\out2.mp3"));
  34.                
  35.                 int by = 0;

  36.                 //System.out.println("第一个字节:"+bufis.myRead());

  37.                 while((by=bufis.myRead())!=-1)
  38.                 {
  39.                         bufos.write(by);
  40.                 }

  41.                 bufos.close();
  42.                 bufis.myClose();
  43.         }

  44.         //通过字节流的缓冲区完成复制。
  45.         public static void copy_1()throws IOException
  46.         {
  47.                 BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("d:\\in.mp3"));
  48.                 BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("d:\\out1.mp3"));
  49.                
  50.                 int by = 0;

  51.                 while((by=bufis.read())!=-1)
  52.                 {
  53.                         bufos.write(by);
  54.                 }

  55.                 bufos.close();
  56.                 bufis.close();

  57.                
  58.         }
  59. }
复制代码

昨天看到IO部分,就想我不使用缓冲区,直接写出去能怎么样,然后就自己加上了函数copy_3,然后测试了一下copy_2与copy_3的耗时,明显平均速度还是毕老师使用缓冲区技术快了几毫秒。
不明白具体快在了哪里。。。

8 个回复

倒序浏览
你问我天有多高,我只能告诉你很高很高...
回复 使用道具 举报
本帖最后由 as604049322 于 2014-12-3 11:21 编辑

下面是MyBufferedInputStream类的代码:
  1. class MyBufferedInputStream
  2. {
  3.         private InputStream in;

  4.         private byte[] buf = new byte[1024*4];
  5.                
  6.         private int pos = 0,count = 0;
  7.         
  8.         MyBufferedInputStream(InputStream in)
  9.         {
  10.                 this.in = in;
  11.         }

  12.         //一次读一个字节,从缓冲区(字节数组)获取。
  13.         public int myRead()throws IOException
  14.         {
  15.                 //通过in对象读取硬盘上数据,并存储buf中。
  16.                 if(count==0)
  17.                 {
  18.                         count = in.read(buf);
  19.                         if(count<0)
  20.                                 return -1;
  21.                         pos = 0;
  22.                 }
  23.                 if(count>=0)
  24.                 {
  25.                         byte b = buf[pos];

  26.                         count--;
  27.                         pos++;
  28.                         return b&0xff;
  29.                 }
  30.                 return -1;

  31.         }
  32.         public void myClose()throws IOException
  33.         {
  34.                 in.close();
  35.         }
  36. }
复制代码


回复 使用道具 举报
你要明白I/O的输出对系统来说其实是一个中断操作,那使用缓冲技术就是减少了高频率的中断,那么时间和性能上肯定是会提高的。
回复 使用道具 举报
不明白,
回复 使用道具 举报
事实证明自定义一个数组比加缓冲区快得多
回复 使用道具 举报
wtjohn 中级黑马 2014-12-3 12:17:09
7#
{:2_32:} 说一个简单的,水缸里有水,需要你把水全倒出来,你可以使用勺子,也可以使用水桶,你说哪个快{:2_43:}
回复 使用道具 举报
缓冲区技术不是就是在内部封装了一个数组吗
回复 使用道具 举报
cbb 中级黑马 2014-12-3 20:41:38
9#
记得有一道题就是:怎么高效复制文件!却是使用字符数组最快!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马