[Java] 文件操作--复制文件夹 →→→→→进入此内容的聊天室

来自 , 2021-03-12, 写在 Java, 查看 116 次.
URL http://www.code666.cn/view/655ea4bd
  1.         public static void CopyFolder(String oldPath, String newPath) {
  2.  
  3.                 try {
  4.                         (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
  5.                         File a = new File(oldPath);
  6.                         String[] file = a.list();
  7.                         File temp = null;
  8.                         for (int i = 0; i < file.length; i++) {
  9.                                 if (oldPath.endsWith(File.separator)) {
  10.                                         temp = new File(oldPath + file[i]);
  11.                                 } else {
  12.                                         temp = new File(oldPath + File.separator + file[i]);
  13.                                 }
  14.  
  15.                                 if (temp.isFile()) {
  16.                                         FileInputStream input = new FileInputStream(temp);
  17.                                         FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString());
  18.                                         byte[] b = new byte[1024 * 5];
  19.                                         int len;
  20.                                         while ((len = input.read(b)) != -1) {
  21.                                                 output.write(b, 0, len);
  22.                                         }
  23.                                         output.flush();
  24.                                         output.close();
  25.                                         input.close();
  26.                                 }
  27.                                 if (temp.isDirectory()) {// 如果是子文件夹
  28.                                         CopyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
  29.                                 }
  30.                         }
  31.                 } catch (Exception e) {
  32.                         System.out.println("复制整个文件夹内容操作出错");
  33.                         e.printStackTrace();
  34.                 }
  35.         }

回复 "文件操作--复制文件夹"

这儿你可以回复上面这条便签

captcha