package com.hssx.centerdata.util;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author: 吴涛涛 cuiyi@itany.com
 * Date : 2019 - 08 - 30 13:59
 * Description:<描述>
 * Version: 1.0
 */
public class FileCopyToFolderUtil {
    public static void main(String[] args) throws IOException {
        Date date = new Date();
        long time1 = date.getTime();
        List<String>  sourceFileUrls = new ArrayList<>();
        sourceFileUrls.add("D:\\软件\\ideaIU-2018.1.5.exe");
        copy(sourceFileUrls, "D:\\775");
        date = new Date();
        long time2 = date.getTime();
        System.out.println("耗时===》"+(time2-time1)/1000);
        File file = new File("D:\\776");
        if (file.exists()) {
            file.delete();
            //创建文件夹
            file.mkdirs();
        } else {
            file.mkdirs();
        }
        System.out.println(file.getPath());
    }
    public static String copy(List<String>  sourceFileUrls, String destinationFolder) throws IOException {
        //新文件夾
        File file = new File(destinationFolder);
        if (file.exists()) {
            file.delete();
            //创建文件夹
            file.mkdirs();
        } else {
            file.mkdirs();
        }
        //如果源文件存在就复制
        for (String sourceFileUrl : sourceFileUrls) {
            //目标源文件夹
            File source = new File(sourceFileUrl);
            if (source.exists()) {
                //新文件夹的路径
                File newFile = new File(file + File.separator + source.getName());
                if (source.isFile()) {
                    FileInputStream in = new FileInputStream(source);
                    BufferedInputStream bis= new BufferedInputStream(in);
                    FileOutputStream out = new FileOutputStream(newFile);
                    BufferedOutputStream bos= new BufferedOutputStream(out);
                    byte[] bs = new byte[4096*10];
                    int count = 0;
//循环把源文件的内容写入新文件
                    while ((count = bis.read(bs, 0, bs.length)) != -1) {
                        bos.write(bs, 0, count);
                    }
//关闭流
                    out.flush();
                    out.close();
                    in.close();
                }
            }
        }
        return file.getPath();
    }

}
