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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 曹昌豪 中级黑马   /  2012-5-21 18:59  /  1910 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.io.*;
//用不同的方法复制文件并统计执行时间
public class CopyFile_2 {
        
        public static void main(String[] args) throws IOException {
               
                long start = System.currentTimeMillis();
                copyFile_1();
                long end = System.currentTimeMillis();
                System.out.println(end-start+"毫秒");
               
                start = System.currentTimeMillis();
                copyFile_2();
                end = System.currentTimeMillis();
                System.out.println(end-start+"毫秒");
               
                start = System.currentTimeMillis();
                copyFile_3();
                end = System.currentTimeMillis();
                System.out.println(end-start+"毫秒");
               
                start = System.currentTimeMillis();
                copyFile_4();
                end = System.currentTimeMillis();
                System.out.println(end-start+"毫秒");
        }
        
        //基本读取方式
        public static void copyFile_1() throws IOException        {
                //创建IO流对象
                FileInputStream fis = new FileInputStream("D:\\Kugou\\in\\xuewei - jia.mp3");
                FileOutputStream fos = new FileOutputStream("jia1.mp3");
               
                int data = 0;
                //循环读写数据
                while ((data=fis.read())!=-1) {
                        fos.write(data);
                }
                        fis.close();
                        fos.close();               
        }
        
        //在基本读取的方式定义字节数组作为缓冲
        public static void copyFile_2() throws IOException        {
                //创建IO流对象
                FileInputStream fis = new FileInputStream("D:\\Kugou\\in\\xuewei - jia.mp3");
                FileOutputStream fos = new FileOutputStream("jia2.mp3");
               
                byte[] buf = new byte[1024];
                int len = 0;
                //循环读写数据
                while ((len=fis.read(buf))!=-1) {
                        fos.write(buf,0,len);
                }
                        fis.close();
                        fos.close();                        
        }
        
        //通过包装类来读取和写入文件
        public static void copyFile_3() throws IOException {
                //创建IO流对象
                BufferedInputStream fis = new BufferedInputStream(new FileInputStream("D:\\Kugou\\in\\xuewei - jia.mp3"));
                BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream("jia3.mp3"));
               
                int data = 0;
                //循环读写数据
                while ((data=fis.read())!= -1) {
                        fos.write(data);
                }
                        fis.close();
                        fos.close();               
        }
        
        //在使用包装类也同时使用数组缓冲
        public static void copyFile_4() throws IOException {
                //创建IO流对象
                BufferedInputStream fis = new BufferedInputStream(new FileInputStream("D:\\Kugou\\in\\xuewei - jia.mp3"));
                BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream("jia4.mp3"));
               
                int data = 0;
                byte[] buf = new byte[1024];
                //循环读写数据
                while ((data=fis.read(buf))!= -1) {
                        fos.write(buf,0,data);                                    
                }
                        fis.close();
                        fos.close();               
        }   
}
运行结果
59578毫秒
79毫秒
89毫秒
24毫秒

//为何第四种方式比较块呢,而且在包装类中已经封装得有数组,为何还需要在外面再定义一个数组呢?第一种方式为什么会比其他三种方式慢这么多?不解,请指教,先谢谢了。

评分

参与人数 1技术分 +1 收起 理由
贠(yun)靖 + 1 赞一个!

查看全部评分

1 个回复

倒序浏览
其实缓冲区好比是一个小容器,数组是一个比缓冲区稍大的容器,就好比你手动复制一篇文章,第一个方法是你一次复制一个字粘贴一个字,这样当然慢了,第二个是你一次复制一个数组容器容量的数据再一次性粘出去,速度较快,第三个是你一次复制资格缓冲区容器容量的数据再一次性粘出去,第四个是一次往一个数组容器中加入一个缓冲区容量的数据(第二种是一次往数组里加一个字节的数据加满后在一次写出去),再一次把数组容器里的数据写出去,速度最快。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马