Java实现复制文件或者文件夹

 拷贝一个文件的算法比较简单,当然,可以对它进行优化,比如使用缓冲流,提高读写数据的效率等。

 话不多说直接上代码

import java.io.*;

/**
 * 实现文件的拷贝
 */
public class CopyFile {

    /**
     * 复制文件夹
     *
     * @param resource 源路径
     * @param target   目标路径
     */
    public static void copyFolder(String resource, String target) throws Exception {

        File resourceFile = new File(resource);
        if (!resourceFile.exists()) {
            throw new Exception("源目标路径:[" + resource + "] 不存在...");
        }
        File targetFile = new File(target);
        if (!targetFile.exists()) {
            throw new Exception("存放的目标路径:[" + target + "] 不存在...");
        }

        // 获取源文件夹下的文件夹或文件
        File[] resourceFiles = resourceFile.listFiles();

        for (File file : resourceFiles) {

            File file1 = new File(targetFile.getAbsolutePath() + File.separator + resourceFile.getName());
            // 复制文件
            if (file.isFile()) {
                System.out.println("文件" + file.getName());
                // 在 目标文件夹(B) 中 新建 源文件夹(A),然后将文件复制到 A 中
                // 这样 在 B 中 就存在 A
                if (!file1.exists()) {
                    file1.mkdirs();
                }
                File targetFile1 = new File(file1.getAbsolutePath() + File.separator + file.getName());
                copyFile(file, targetFile1);
            }
            // 复制文件夹
            if (file.isDirectory()) {// 复制源文件夹
                String dir1 = file.getAbsolutePath();
                // 目的文件夹
                String dir2 = file1.getAbsolutePath();
                copyFolder(dir1, dir2);
            }
        }

    }

    /**
     * 复制文件
     *
     * @param resource
     * @param target
     */
    public static void copyFile(File resource, File target) throws Exception {
        // 输入流 --> 从一个目标读取数据
        // 输出流 --> 向一个目标写入数据

        long start = System.currentTimeMillis();

        // 文件输入流并进行缓冲
        FileInputStream inputStream = new FileInputStream(resource);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

        // 文件输出流并进行缓冲
        FileOutputStream outputStream = new FileOutputStream(target);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);

        // 缓冲数组
        // 大文件 可将 1024 * 2 改大一些,但是 并不是越大就越快
        byte[] bytes = new byte[1024 * 2];
        int len = 0;
        while ((len = inputStream.read(bytes)) != -1) {
            bufferedOutputStream.write(bytes, 0, len);
        }
        // 刷新输出缓冲流
        bufferedOutputStream.flush();
        //关闭流
        bufferedInputStream.close();
        bufferedOutputStream.close();
        inputStream.close();
        outputStream.close();

        long end = System.currentTimeMillis();

        System.out.println("耗时:" + (end - start) / 1000 + " s");

    }


    // 使用示例
    public static void main(String[] args) {

        String rootPath = LoggerUtil.getJarRootPath();
        // rootPath = "E:\MyProject\student\target\classes";

        System.out.println("--------------------------------复制文件-------------------------------------------");

        File f1 = new File("D:\\GHO\\Windows10企业版.iso");
        // 目标文件
        File f2 = new File("F:\\logs\\" + "win10.iso");

        try {
            // 这个 win10系统 大概是 3.50G 的 复制过程 花了 156 秒 == 2 分6 秒
            copyFile(f1, f2);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("--------------------------------复制文件夹-------------------------------------------");

        String resource = rootPath + "logs" + File.separator + "job1234";
        String target = rootPath + "logs" + File.separator + "job123";
        try {
            copyFolder(resource, target);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

 

posted @ 2019-06-11 14:02  追梦滴小蜗牛  阅读(14409)  评论(1编辑  收藏  举报