FileCopyToFolderUtil.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package com.hssx.centerdata.util;
  2. import java.io.*;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.List;
  6. /**
  7. * Author: 吴涛涛 cuiyi@itany.com
  8. * Date : 2019 - 08 - 30 13:59
  9. * Description:<描述>
  10. * Version: 1.0
  11. */
  12. public class FileCopyToFolderUtil {
  13. public static void main(String[] args) throws IOException {
  14. Date date = new Date();
  15. long time1 = date.getTime();
  16. List<String> sourceFileUrls = new ArrayList<>();
  17. sourceFileUrls.add("D:\\软件\\ideaIU-2018.1.5.exe");
  18. copy(sourceFileUrls, "D:\\775");
  19. date = new Date();
  20. long time2 = date.getTime();
  21. System.out.println("耗时===》"+(time2-time1)/1000);
  22. File file = new File("D:\\776");
  23. if (file.exists()) {
  24. file.delete();
  25. //创建文件夹
  26. file.mkdirs();
  27. } else {
  28. file.mkdirs();
  29. }
  30. System.out.println(file.getPath());
  31. }
  32. public static String copy(List<String> sourceFileUrls, String destinationFolder) throws IOException {
  33. //新文件夾
  34. File file = new File(destinationFolder);
  35. if (file.exists()) {
  36. file.delete();
  37. //创建文件夹
  38. file.mkdirs();
  39. } else {
  40. file.mkdirs();
  41. }
  42. //如果源文件存在就复制
  43. for (String sourceFileUrl : sourceFileUrls) {
  44. //目标源文件夹
  45. File source = new File(sourceFileUrl);
  46. if (source.exists()) {
  47. //新文件夹的路径
  48. File newFile = new File(file + File.separator + source.getName());
  49. if (source.isFile()) {
  50. FileInputStream in = new FileInputStream(source);
  51. BufferedInputStream bis= new BufferedInputStream(in);
  52. FileOutputStream out = new FileOutputStream(newFile);
  53. BufferedOutputStream bos= new BufferedOutputStream(out);
  54. byte[] bs = new byte[4096*10];
  55. int count = 0;
  56. //循环把源文件的内容写入新文件
  57. while ((count = bis.read(bs, 0, bs.length)) != -1) {
  58. bos.write(bs, 0, count);
  59. }
  60. //关闭流
  61. out.flush();
  62. out.close();
  63. in.close();
  64. }
  65. }
  66. }
  67. return file.getPath();
  68. }
  69. }