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

今天药忘吃喽~ 2023-06-19 09:36 100阅读 0赞

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

话不多说直接上代码

  1. import java.io.*;
  2. /**
  3. * 实现文件的拷贝
  4. */
  5. public class CopyFile {
  6. /**
  7. * 复制文件夹
  8. *
  9. * @param resource 源路径
  10. * @param target 目标路径
  11. */
  12. public static void copyFolder(String resource, String target) throws Exception {
  13. File resourceFile = new File(resource);
  14. if (!resourceFile.exists()) {
  15. throw new Exception("源目标路径:[" + resource + "] 不存在...");
  16. }
  17. File targetFile = new File(target);
  18. if (!targetFile.exists()) {
  19. throw new Exception("存放的目标路径:[" + target + "] 不存在...");
  20. }
  21. // 获取源文件夹下的文件夹或文件
  22. File[] resourceFiles = resourceFile.listFiles();
  23. for (File file : resourceFiles) {
  24. File file1 = new File(targetFile.getAbsolutePath() + File.separator + resourceFile.getName());
  25. // 复制文件
  26. if (file.isFile()) {
  27. System.out.println("文件" + file.getName());
  28. // 在 目标文件夹(B) 中 新建 源文件夹(A),然后将文件复制到 A 中
  29. // 这样 在 B 中 就存在 A
  30. if (!file1.exists()) {
  31. file1.mkdirs();
  32. }
  33. File targetFile1 = new File(file1.getAbsolutePath() + File.separator + file.getName());
  34. copyFile(file, targetFile1);
  35. }
  36. // 复制文件夹
  37. if (file.isDirectory()) {// 复制源文件夹
  38. String dir1 = file.getAbsolutePath();
  39. // 目的文件夹
  40. String dir2 = file1.getAbsolutePath();
  41. copyFolder(dir1, dir2);
  42. }
  43. }
  44. }
  45. /**
  46. * 复制文件
  47. *
  48. * @param resource
  49. * @param target
  50. */
  51. public static void copyFile(File resource, File target) throws Exception {
  52. // 输入流 --> 从一个目标读取数据
  53. // 输出流 --> 向一个目标写入数据
  54. long start = System.currentTimeMillis();
  55. // 文件输入流并进行缓冲
  56. FileInputStream inputStream = new FileInputStream(resource);
  57. BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
  58. // 文件输出流并进行缓冲
  59. FileOutputStream outputStream = new FileOutputStream(target);
  60. BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
  61. // 缓冲数组
  62. // 大文件 可将 1024 * 2 改大一些,但是 并不是越大就越快
  63. byte[] bytes = new byte[1024 * 2];
  64. int len = 0;
  65. while ((len = inputStream.read(bytes)) != -1) {
  66. bufferedOutputStream.write(bytes, 0, len);
  67. }
  68. // 刷新输出缓冲流
  69. bufferedOutputStream.flush();
  70. //关闭流
  71. bufferedInputStream.close();
  72. bufferedOutputStream.close();
  73. inputStream.close();
  74. outputStream.close();
  75. long end = System.currentTimeMillis();
  76. System.out.println("耗时:" + (end - start) / 1000 + " s");
  77. }
  78. // 使用示例
  79. public static void main(String[] args) {
  80. String rootPath = LoggerUtil.getJarRootPath();
  81. // rootPath = "E:\MyProject\student\target\classes";
  82. System.out.println("--------------------------------复制文件-------------------------------------------");
  83. File f1 = new File("D:\\GHO\\Windows10企业版.iso");
  84. // 目标文件
  85. File f2 = new File("F:\\logs\\" + "win10.iso");
  86. try {
  87. // 这个 win10系统 大概是 3.50G 的 复制过程 花了 156 秒 == 2 分6 秒
  88. copyFile(f1, f2);
  89. } catch (Exception e) {
  90. e.printStackTrace();
  91. }
  92. System.out.println("--------------------------------复制文件夹-------------------------------------------");
  93. String resource = rootPath + "logs" + File.separator + "job1234";
  94. String target = rootPath + "logs" + File.separator + "job123";
  95. try {
  96. copyFolder(resource, target);
  97. } catch (Exception e) {
  98. e.printStackTrace();
  99. }
  100. }
  101. }

发表评论

表情:
评论列表 (有 0 条评论,100人围观)

还没有评论,来说两句吧...

相关阅读

    相关 golang 复制 文件 文件夹

    在golang 中,复制文件是比较容易的,直接操作"io/ioutil"包就可以了,但是复制文件夹就有点麻烦了,需要一层层检查是否存在这个文件夹,不存在就创建文件夹, 实例如