黑马程序员技术交流社区

标题: IO复制小问题求解 [打印本页]

作者: 黄杨    时间: 2013-2-3 23:15
标题: IO复制小问题求解
本帖最后由 张向辉 于 2013-2-6 14:43 编辑

public class copyTu {
/**
  * @param args
  */
public static void main(String[] args) throws IOException {
  
  long start=System.currentTimeMillis();
  copy();
  long end=System.currentTimeMillis();
  System.out.println((end-start)+"毫秒");
  
}

public static void copy() throws IOException {
  FileInputStream fis=null;
  FileOutputStream fos=null;
  
  fis=new FileInputStream("d:\\My Documents\\My Pictures\\1.jpg");
  fos=new FileOutputStream("d:\\My Documents\\My Pictures\\2.jpg");
   
  byte[] b=new byte[1024];
  int len=0;
  while ((len=fis.read(b))!=-1) {
   fos.write(b, 0, len);
  }
}
}

这代码和老师讲的差不多,编译没问题,但是运行的时候都是打印0毫秒,不知道什么原因。。。

作者: 夏振博    时间: 2013-2-3 23:31
没问题啊,顺便说声你类名的首字符没大写
作者: 陈科宇    时间: 2013-2-3 23:57
     朋友,这是计算机的时间精度问题,和你的程序没有关系。我测试过几次,15秒,16秒。偶尔会出现0毫秒的情形。假定计算机的精度就是15毫秒,而你某一次运行的时间小于15毫秒,那么你看到的就有可能是0毫秒。希望我的解答能使你满意。




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