IO流-文件的复制

1、字节流复制文件:

    public void myTest() {
        // 定义输入和输出流
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("E:\\电影\\黄飞鸿之铁鸡斗蜈蚣.mkv");
            fos = new FileOutputStream("F:\\黄飞鸿之铁鸡斗蜈蚣.mkv");
            byte[] b = new byte[1024];
            int len = 0;
            long start = System.currentTimeMillis();// 记录开始复制的时间
            while ((len = fis.read(b)) != -1) {
                fos.write(b, 0, len);
            }
            long end = System.currentTimeMillis();// 记录结束复制的时间
            System.out.println(end - start);// 算出复制文件所用的时间(ms)
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

2、

    public void myTest() {
        // 定义输入和输出流
        BufferedInputStream bfis = null;
        BufferedOutputStream bfos = null;
        try {
            bfis = new BufferedInputStream(new FileInputStream("E:\\电影\\黄飞鸿之铁鸡斗蜈蚣.mkv"));
            bfos = new BufferedOutputStream(new FileOutputStream("F:\\黄飞鸿之铁鸡斗蜈蚣.mkv"));
            byte[] b = new byte[1024];
            int len = 0;
            long start = System.currentTimeMillis();// 记录开始复制的时间
            while ((len = bfis.read(b)) != -1) {
                bfos.write(b, 0, len);
            }
            long end = System.currentTimeMillis();// 记录结束复制的时间
            System.out.println(end - start);// 算出复制文件所用的时间(ms)
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bfos != null) {
                    bfos.close();
                }
                if (bfis != null) {
                    bfis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 

posted @ 2017-11-19 16:14  小蘇  阅读(138)  评论(0编辑  收藏  举报