zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
  • 相关阅读:
    【模板】常系数线性递推
    【模板】多项式除法
    【模板】多项式求逆
    codeblock的一个小问题
    IP分片浅析
    [LeetCode] Excel Sheet Column Number
    [LeetCode] Excel Sheet Column Title
    [LeetCode] Maximum Gap
    编程计算并输出1~n之间所有素数之和
    [LeetCode] Compare Version Numbers
  • 原文地址:https://www.cnblogs.com/a591378955/p/7860130.html
Copyright © 2011-2022 走看看