/** * Copies source to destination. If source and destination are the same, does nothing. Both single files and * directories are handled. * * @param src the source file or directory * @param dst the destination file or directory */ public static void copy(File src, File dst) throws IOException { if (src.equals(dst)) { return; } if (src.isFile()) { copyFile(src, dst); } else { copyTree(src, dst); } }
/** * Copies recursively source to destination. * <p> * The source file is assumed to be a directory. * * @param src the source directory * @param dst the destination directory * @deprecated since 10.1 - waiting ReloadComponent to be cleaned */ @Deprecated public static void copyTree(File src, File dst) throws IOException { if (src.isFile()) { copyFile(src, dst); } else if (src.isDirectory()) { if (dst.exists()) { dst = new File(dst, src.getName()); dst.mkdir(); } else { // allows renaming dest dir dst.mkdirs(); } File[] files = src.listFiles(); for (File file : files) { copyTree(file, dst); } } }
protected static void copyTree(int rootIndex, File src, File dst, Path prefix, PathFilter filter) throws IOException { if (src.isFile()) { String relPath = src.getPath().substring(rootIndex); if (!filter.accept(new Path(relPath))) { return; } if (!prefix.isRoot()) { // remove prefix from path String path = dst.getPath(); String pff = prefix.toString(); int prefixIndex = path.lastIndexOf(pff); if (prefixIndex > 0) { path = path.substring(0, prefixIndex) + path.substring(prefixIndex + pff.length()); dst = new File(path.toString()); } } dst.getParentFile().mkdirs(); copyFile(src, dst); } else if (src.isDirectory()) { File[] files = src.listFiles(); for (File file : files) { copyTree(rootIndex, file, new File(dst, file.getName()), prefix, filter); } } }