黑马程序员技术交流社区

标题: 为什么每次运行时间会相差很大 [打印本页]

作者: guojiadui    时间: 2015-5-16 08:50
标题: 为什么每次运行时间会相差很大
public class Test11 {

         
        public static void main(String[] args) {
               
                long start = System.currentTimeMillis();
                myCopy();
                long end = System.currentTimeMillis();
                //运行时间
                System.out.println(end-start+"毫秒");
        }
         public static void myCopy()
         {
                 Reader read =null;
                 Writer writer = null;
                 try {
                         // 生成读写流对象,指定要操作的文件。
                          read = new FileReader("d:\\1.txt");
                          writer = new FileWriter("d:\\copy.txt");
                          char[] chs =new char[100];
                          int len = 0;
                          //复制文件
                          while((len=read.read(chs))!=-1)
                          {
                                  writer.write(chs,0,len);
                                  writer.flush();
                          }
                          
                } catch ( IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }finally
                {
                        if(read!=null)
                                try {
                                        read.close();
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                        if(writer!=null)
                                try {
                                        writer.close();
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                }
                 
         }
         
}





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