如何递归复制整个目录,包括Java中的父文件夹

我目前正在将文件夹从一个地方复制到另一个地方。 它工作正常,但它没有复制原始文件夹,所有其余的文件和文件夹也在其中。 这是我正在使用的代码:

public static void copyFolder(File src, File dest) throws IOException { if (src.isDirectory()) { //if directory not exists, create it if (!dest.exists()) { dest.mkdir(); } //list all the directory contents String files[] = src.list(); for (String file : files) { //construct the src and dest file structure File srcFile = new File(src, file); File destFile = new File(dest+"\\"+src.getName(), file); //recursive copy copyFolder(srcFile,destFile); } } else { //if file, then copy it //Use bytes stream to support all file types InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest); byte[] buffer = new byte[1024]; int length; //copy the file content in bytes while ((length = in.read(buffer)) > 0){ out.write(buffer, 0, length); } in.close(); out.close(); System.out.println("File copied from " + src + " to " + dest); } } 

所以我有文件夹src C:\test\mytest\..all folders..

我想将它复制到C:\test\myfiles

但不是得到C:\test\myfiles\mytest\..all folders..我得到C:\test\myfiles\..all folders..

尝试使用Apache Commons IO库中的copyDirectory(File srcDir,File destDir)方法。

有一个教程,用于 在Oracle Docs上 使用带有递归复制示例代码的 java.nio复制文件 。 它适用于java se 7+。 它使用Files.walkFileTree方法,这可能会在带有连接点的ntfs上引起一些问题 。 为避免使用Files.walkFileTree,可能的解决方案如下所示:

 public static void copyFileOrFolder(File source, File dest, CopyOption... options) throws IOException { if (source.isDirectory()) copyFolder(source, dest, options); else { ensureParentFolder(dest); copyFile(source, dest, options); } } private static void copyFolder(File source, File dest, CopyOption... options) throws IOException { if (!dest.exists()) dest.mkdirs(); File[] contents = source.listFiles(); if (contents != null) { for (File f : contents) { File newFile = new File(dest.getAbsolutePath() + File.separator + f.getName()); if (f.isDirectory()) copyFolder(f, newFile, options); else copyFile(f, newFile, options); } } } private static void copyFile(File source, File dest, CopyOption... options) throws IOException { Files.copy(source.toPath(), dest.toPath(), options); } private static void ensureParentFolder(File file) { File parent = file.getParentFile(); if (parent != null && !parent.exists()) parent.mkdirs(); } 

您也可以尝试Apache FileUtils来复制目录

你应该尝试apache commons FileUtils

使用java.nio:

 import java.io.IOException; import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; public static void copy(String sourceDir, String targetDir) throws IOException { abstract class MyFileVisitor implements FileVisitor { boolean isFirst = true; Path ptr; } MyFileVisitor copyVisitor = new MyFileVisitor() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { // Move ptr forward if (!isFirst) { // .. but not for the first time since ptr is already in there Path target = ptr.resolve(dir.getName(dir.getNameCount() - 1)); ptr = target; } Files.copy(dir, ptr, StandardCopyOption.COPY_ATTRIBUTES); isFirst = false; return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Path target = ptr.resolve(file.getFileName()); Files.copy(file, target, StandardCopyOption.COPY_ATTRIBUTES); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { throw exc; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Path target = ptr.getParent(); // Move ptr backwards ptr = target; return FileVisitResult.CONTINUE; } }; copyVisitor.ptr = Paths.get(targetDir); Files.walkFileTree(Paths.get(sourceDir), copyVisitor); } 

当然Spring也包含了FileSystemUtils.copyRecursively(File src,File dest)

主要问题是:

  dest.mkdir(); 

只创建一个目录,而不是父目录,在第一步之后你需要创建两个目录,所以用mkdirs替换mkdir 。 之后我猜你会因为你的递归而有重复的子目录(类似于C:\ test \ myfiles \ mytest \ dir1 \ dir1 \ subdir1 \ subdir1 …),所以也尝试修复这一行:

  File destFile = new File(dest, src.getName()); /**/ OutputStream out = new FileOutputStream(new File(dest, src.getName())); 

此代码将文件夹从源复制到目标:

  public static void copyDirectory(String srcDir, String dstDir) { try { File src = new File(srcDir); String ds=new File(dstDir,src.getName()).toString(); File dst = new File(ds); if (src.isDirectory()) { if (!dst.exists()) { dst.mkdir(); } String files[] = src.list(); int filesLength = files.length; for (int i = 0; i < filesLength; i++) { String src1 = (new File(src, files[i]).toString()); String dst1 = dst.toString(); copyDirectory(src1, dst1); } } else { fileWriter(src, dst); } } catch (Exception e) { e.printStackTrace(); } } public static void fileWriter(File srcDir, File dstDir) throws IOException { try { if (!srcDir.exists()) { System.out.println(srcDir + " doesnot exist"); throw new IOException(srcDir + " doesnot exist"); } else { InputStream in = new FileInputStream(srcDir); OutputStream out = new FileOutputStream(dstDir); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } } catch (Exception e) { } }