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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

代码有点长,不过加了点注释。这是看视频后的练习,跟老师的代码比较也没发现什么不一样,但就是比老师的运行时间长,复制个500多kb的歌曲跑了12秒,:time:检查半天没发现是哪 的问题,各位帮看哈
  1. import java.io.IOException;
  2. import java.io.InputStream;

  3. public class myBufferedInputStream {
  4.         private InputStream in;
  5.         private byte[] buf = new byte[1024];
  6.         private int count = 0, pos = 0;

  7.         myBufferedInputStream(InputStream in) {
  8.                 this.in = in;
  9.         }
  10.         public int myRead() throws IOException {
  11.                 if (count == 0) {
  12.                         count = in.read(buf);//这里是一次读满一组数组吧??那count为1024
  13.                         if (count < 0) {
  14.                                 return -1;
  15.                         }
  16.                         pos = 0;//指针归零
  17.                         byte b = buf[pos];//按个取出,这里取buf[0]吧
  18.                         count--;//总数减一个
  19.                         pos++;//指针移位
  20.                         // System.out.println(b);
  21.                         return b * 255;//将将int转换为byte
  22.                 }
  23.                 if (count > 0) {
  24.                         byte b = buf[pos];//数组还没取完
  25.                         count--;
  26.                         pos++;
  27.                         return b * 0xff;//将将int转换为byte
  28.                 }
  29.                 return -1;
  30.         }
  31.         public void myClose() {
  32.                 try {
  33.                         in.close();
  34.                 } catch (IOException e) {
  35.                         // TODO Auto-generated catch block
  36.                         e.printStackTrace();
  37.                 }
  38.         }
  39. }
  40. import java.io.*;

  41. public class TestBufInps {
  42.         public static void main(String[] args) {
  43.                 long start = System.currentTimeMillis();//开始时间
  44.                 myBufferedInputStream myBuf = null;//自定义缓冲
  45.                 OutputStream out = null;
  46.                 try {
  47.                         myBuf = new myBufferedInputStream(new FileInputStream("d:\\13.mp3"));
  48.                         out = new FileOutputStream("d:\\14.mp3");//指定位置
  49.                         int len = 0;
  50.                         while ((len = myBuf.myRead()) != -1) {//调用自己的read方法
  51.                                 out.write(len);//写入指定位置,这里有一个自动转换为的操作
  52.                         }
  53.                         out.flush();//保存
  54.                 } catch (IOException e) {
  55.                         throw new RuntimeException("Error");
  56.                 } finally {
  57.                         myBuf.myClose();//关闭流也就关闭了缓冲
  58.                         try {
  59.                                 out.close();//关流
  60.                         } catch (IOException e) {
  61.                                 System.out.println(e);
  62.                         }
  63.                 }
  64.                 long end = System.currentTimeMillis();
  65.                 System.out.println("time" + (end - start)/1000);//复制500多kb竟然用12秒
  66.         }
  67. }
复制代码

4 个回复

正序浏览
黑马王建伟 发表于 2012-9-7 16:08
同学,看了你的代码,感觉别扭,你看我的代码,没有try,直接throws的,运行时间的话,很短,你可以复制代 ...

我看了,这样是直接调用FileInputStream的read,所以它会很快,我这个是学习老师那样自己模拟缓冲区也就是模拟BufferedInputStream的实现。谢谢分析,加油。
回复 使用道具 举报
本帖最后由 张飞年 于 2012-9-7 16:31 编辑

自己顶哈。
回复 使用道具 举报
我看了,这样是直接调用FileInputStream的read,所以它会很快,我这个是学习老师那样自己模拟缓冲区也就是模拟BufferedInputStream的实现。谢谢分析,加油。
回复 使用道具 举报
本帖最后由 黑马王建伟 于 2012-9-7 16:11 编辑

同学,看了你的代码,感觉别扭,你看我的代码,没有try,直接throws的,运行时间的话,很短,你可以复制代码试下

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class LianXi{
public static void main(String[] args) throws IOException {
  Long start=System.currentTimeMillis();
  FileInputStream fis=new FileInputStream("g:/java/a.mp3");
  FileOutputStream fos=new FileOutputStream("g:/java/b.mp3");
  byte[]b=new byte[1024];
  int num=0;
  while(-1!=(num=fis.read(b))){
   fos.write(b,0,num);
   fos.flush();
  }
  fis.close();
  fos.close();
  Long end=System.currentTimeMillis();
  System.out.println("复制一个文件所有的时间"+(end-start)+"毫秒");
}
}

未命名.jpg (15 KB, 下载次数: 39)

未命名.jpg

未命名.jpg (15.69 KB, 下载次数: 35)

未命名.jpg
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马