黑马程序员技术交流社区

标题: IO复制文件夹(包含文件夹和文件)---写时发现没想的那么简单 [打印本页]

作者: suihs11    时间: 2015-3-14 11:35
标题: IO复制文件夹(包含文件夹和文件)---写时发现没想的那么简单
本帖最后由 suihs11 于 2015-3-14 13:08 编辑

基础视频中讲过递归删除文件夹,但没讲过复制,自己做的时候遇到了一些问题,但是总体思路是一样的----上代码:

  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;

  7. /**
  8. * 复制文件夹练,并计算出复制的时间
  9. *
  10. * @author 隋
  11. */
  12. public class FileCopy {
  13.         public static void main(String[] args) {
  14.                 // 创建源文件和目的地文件对象
  15.                 File srcFile = new File("D:\\KuGou");
  16.                 File destFile = new File("E:\\");

  17.                 // 记录开始时间
  18.                 long start = System.currentTimeMillis();
  19.                 // 调用复制文件夹方法
  20.                 copyDirectory(srcFile, destFile);
  21.                 // 记录结束时间
  22.                 long end = System.currentTimeMillis();

  23.                 /*
  24.                  * //这样写结果多了8小时,应该是时区的问题,不会弄
  25.                  *
  26.                  * //获取标准时间格式 String pattern = "HH小时mm分钟ss秒";
  27.                  * SimpleDateFormat sdf = new SimpleDateFormat(pattern);
  28.                  * String time = sdf.format(end-start);
  29.                  * System.out.println("复制" + srcFile.getAbsolutePath() + "---到---"
  30.                  *                + destFile.getAbsolutePath() + "共耗时" + time);
  31.                  */
  32.                
  33.                 // 把复制文件耗时打印在控制台上
  34.                 System.out.println("复制" + srcFile.getAbsolutePath() + "---到---"
  35.                                                  + destFile.getAbsolutePath() + "共耗时" + (end - start) + "毫秒" );
  36.         }

  37.         /**
  38.          * 复制文件夹方法
  39.          *
  40.          * @param srcFile
  41.          *            源文件对象
  42.          * @param disFile
  43.          *            目的地文件对象
  44.          */
  45.         public static void copyDirectory(File srcFile, File disFile) {
  46.                 // 定义目的地文件(夹)名字
  47.                 String destFileName = null;
  48.                 // 创建目的地文件夹
  49.                 File destFile = new File(disFile, srcFile.getName());
  50.                 if (!destFile.exists()) {
  51.                         destFile.mkdirs();
  52.                 }
  53.                 // 获取文件夹下面的文件和文件夹列表数组
  54.                 File[] files = srcFile.listFiles();
  55.                 // 遍历数组
  56.                 for (File file : files) {
  57.                         // 判断如果是文件夹,就到目的地文件夹下创建该文件夹,并递归
  58.                         if (file.isDirectory()) {
  59.                                 destFileName = destFile.getAbsolutePath() + "\\" + file.getName();
  60.                                 File directoryFile = new File(destFileName);
  61.                                 directoryFile.mkdirs();
  62.                                 copyDirectory(file, directoryFile);
  63.                         } else {
  64.                                 // 如果是文件,先在目的地文件夹下创建该文件对象,并调用复制文件方法(copyFile)
  65.                                 destFileName = destFile.getAbsolutePath() + "\\" + file.getName();
  66.                                 File finalFile = new File(destFileName);
  67.                                 try {
  68.                                         finalFile.createNewFile();
  69.                                 } catch (IOException ioe) {
  70.                                         ioe.printStackTrace();
  71.                                 }
  72.                                 copyFile(file, finalFile);
  73.                         }
  74.                 }
  75.         }

  76.         /**
  77.          * 复制文件方法
  78.          *
  79.          * @param srcFile
  80.          *            源文件文件对象
  81.          * @param destFile
  82.          *            目标文件对象
  83.          */
  84.         private static void copyFile(File srcFile, File destFile) {
  85.                 BufferedInputStream bis = null;
  86.                 BufferedOutputStream bos = null;
  87.                 try {
  88.                         bis = new BufferedInputStream(new FileInputStream(srcFile));
  89.                         bos = new BufferedOutputStream(new FileOutputStream(destFile));
  90.                         byte[] bys = new byte[1024];
  91.                         int len = 0;
  92.                         while ((len = bis.read(bys)) != -1) {
  93.                                 bos.write(bys, 0, len);
  94.                                 bos.flush();
  95.                         }
  96.                 } catch (IOException ioe) {
  97.                         ioe.printStackTrace();
  98.                 } finally {
  99.                         try {
  100.                                 if (bis != null) {
  101.                                         bis.close();
  102.                                 }
  103.                         } catch (IOException ioe) {
  104.                                 ioe.printStackTrace();
  105.                         }
  106.                         try {
  107.                                 if (bos != null) {
  108.                                         bos.close();
  109.                                 }
  110.                         } catch (IOException ioe) {
  111.                                 ioe.printStackTrace();
  112.                         }
  113.                 }
  114.         }
  115. }
复制代码



FileCopy.rar

1.28 KB, 下载次数: 163


作者: hamesksk    时间: 2015-3-14 14:28
不错,递归不容易理解,自己写出来真不容易。
作者: danlyalex    时间: 2015-6-28 22:09
我就是被这个File类下的创建新的文件夹时名字路径要改为新的路径加老的名字搞混了..递归复制文件夹和文件好麻烦




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2