/** * Copies a directory from one path to another. If the destination directory exists, the file * contents are merged and files from the source directory overwrite files in the destination. */ public static void copyDirectory(@NonNull File from, @NonNull File to) throws IOException { Preconditions.checkArgument(from.isDirectory(), "Source path is not a directory."); Preconditions.checkArgument( !to.exists() || to.isDirectory(), "Destination path exists and is not a directory."); mkdirs(to); File[] children = from.listFiles(); if (children != null) { for (File child : children) { if (child.isFile()) { copyFileToDirectory(child, to); } else if (child.isDirectory()) { copyDirectoryToDirectory(child, to); } else { throw new IllegalArgumentException( "Don't know how to copy file " + child.getAbsolutePath()); } } } }