继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

拷贝目录及子目录的文件到另外一个目录下

冲啊
关注TA
已关注
手记 12
粉丝 5
获赞 605

1.CopyFile类是拷贝单独的一个文件的类,里面有三个静态方法,分别实现拷贝一个文件的要求,其中 批量拷贝(多字节):效率最高,所以应该多使用此中方法,具体测试这个类的效率,由测试类Test1实现。

package com.imooc;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFile {
    /**
     * 拷贝单个文件(单字节):效率最低
     * 
     * @param srcFile
     *            源文件
     * @param destFile
     *            目标文件
     * @throws IOException
     *             IO异常
     */
    public static void copyFileByByte(File srcFile, File destFile) throws IOException {
        if (!srcFile.exists())
            throw new IllegalArgumentException("文件:" + srcFile + "不存在.");
        if (!srcFile.isFile())
            throw new IllegalArgumentException(srcFile + "不是文件.");

        // 输入流 (读)
        FileInputStream in = new FileInputStream(srcFile);

        // 输出流 (写)
        FileOutputStream out = new FileOutputStream(destFile);
        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
            out.flush();
        }

        // 先关闭输入流,再关闭输出流、
        in.close();
        out.close();

    }

    /**
     * 批量拷贝(多字节):效率最高
     * 
     * @param srcFile
     *            源文件
     * @param destFile
     *            目标文件
     * @throws IOException
     *             IO异常
     */
    public static void copyFileByBytes(File srcFile, File destFile) throws IOException {

        if (!srcFile.exists())
            throw new IllegalArgumentException("文件:" + srcFile + "不存在.");
        if (!srcFile.isFile())
            throw new IllegalArgumentException(srcFile + "不是文件.");

        // 输入流 (读)
        FileInputStream in = new FileInputStream(srcFile);

        // 输出流 (写)
        FileOutputStream out = new FileOutputStream(destFile);

        // 读到的内容以字节的形式存储在字节数组buf中;
        byte[] buf = new byte[100]; // 字节数组(缓冲区)长度100字节
        // 如果读取的源文件大小超过100字节,则表示每读取100字节后,就写入100字节,
        // 但最后一次时,读取的大小应该是(源文件大小减去N*100)<100字节,N表示读的次数,
        // 然后继续读取,直到文件读完毕
        // 如果文件大小小于100字节,则表示一次即可读完文件,并写入新的文件
        int b;
        while ((b = in.read(buf, 0, buf.length)) != -1) {
            out.write(buf, 0, b);
            // System.out.println(b); // 这里可以显示每次读取或写入的字节数
            out.flush(); // 刷新
        }

        // 先关闭输入流,再关闭输出流、
        in.close();
        out.close();
    }

    /**
     * 使用缓冲区拷贝:效率中等
     * 
     * @param srcFile
     *            源文件
     * @param destFile
     *            目标文件
     * @throws IOException
     *             IO异常
     */
    public static void copyFileByBuffered(File srcFile, File destFile) throws IOException {

        if (!srcFile.exists())
            throw new IllegalArgumentException("文件:" + srcFile + "不存在.");
        if (!srcFile.isFile())
            throw new IllegalArgumentException(srcFile + "不是文件.");

        // 输入流 (读)
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));

        // 输出流 (写)
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
        int b;
        while ((b = bis.read()) != -1) {
            bos.write(b);
            bos.flush(); // 刷新
        }

        // 先关闭输入流,再关闭输出流、
        bis.close();
        bos.close();
    }

}

Test1类,测试CopyFile类里的三个静态方法的性能

package com.imooc;

import java.io.File;
import java.io.IOException;

public class Test1 {

    public static void main(String[] args) {
        long begin, end;
        try {
            // 一次读取一个字节(效率最低)
            begin = System.currentTimeMillis();
            CopyFile.copyFileByByte(new File("微信_2.0.0.37.exe"), new File("21.exe"));
            end = System.currentTimeMillis();
            System.out.println("copyFileByByte()" + (end - begin));

            // 一次读取多个字节(效率最高)
            begin = System.currentTimeMillis();
            CopyFile.copyFileByBytes(new File("微信_2.0.0.37.exe"), new File("22.exe"));
            end = System.currentTimeMillis();
            System.out.println("copyFileByBytes()" + (end - begin));

            // 通过缓冲区读取(效率中等)
            begin = System.currentTimeMillis();
            CopyFile.copyFileByBuffered(new File("微信_2.0.0.37.exe"), new File("23.exe"));
            end = System.currentTimeMillis();
            System.out.println("copyFileByByteBuffered()" + (end - begin));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }

}

2、 复制一个目录及其子目录下的所有或目录文件到另外一个目录

package com.imooc;

import java.io.File;
import java.io.IOException;

public class CopyFolder {
    /**
     * 复制一个目录及其子目录、文件到另外一个目录
     * 
     * @param src
     *            源目录或文件
     * @param dest
     *            目标目录或文件
     * @throws IOException异常
     */
    public static void copyFolder(File src, File dest) throws IOException {
        if (src.isDirectory()) {
            if (!dest.exists()) { //目标目录不存在,则创建目录
                dest.mkdir();
            }
            String files[] = src.list(); //返回源目录下的文件及子目录
            for (String file : files) {
                System.out.println("src="+src);
                System.out.println("file="+file);
                File srcFile = new File(src, file); 
                System.out.println("srcFile="+srcFile);
                File destFile = new File(dest, file);
                // 递归调用
                copyFolder(srcFile, destFile);
            }
        } else {// src是文件
            CopyFile.copyFileByBytes(src, dest);
        }
    }
}

Test2是测试类,用于测试CopyFolder类的功能

package com.imooc;

import java.io.File;
import java.io.IOException;

public class Test2 {

    public static void main(String[] args) throws IOException {
        CopyFolder.copyFolder(new File("C:\\docs\\api"), new File("D:\\12"));
    }

}
打开App,阅读手记
14人推荐
发表评论
随时随地看视频慕课网APP

热门评论

拷贝文件目录,递归调用算法有问题

查看全部评论