/** * 拷贝一个目录 * * @param src * 原始目录 * @param target * 新目录 * @return 是否拷贝成功 * @throws IOException */ public static boolean copyDir(File src, File target) throws IOException { if (src == null || target == null || !src.exists()) return false; if (!src.isDirectory()) throw new IOException(src.getAbsolutePath() + " should be a directory!"); if (!target.exists()) if (!makeDir(target)) return false; boolean re = true; File[] files = src.listFiles(); if (null != files) { for (File f : files) { if (f.isFile()) re &= copyFile(f, new File(target.getAbsolutePath() + "/" + f.getName())); else re &= copyDir(f, new File(target.getAbsolutePath() + "/" + f.getName())); } } return re; }
/** * 自动决定是 copy 文件还是目录 * * @param src * 源 * @param target * 目标 * @return 是否 copy 成功 */ public static boolean copy(File src, File target) { try { if (src.isDirectory()) return copyDir(src, target); return copyFile(src, target); } catch (IOException e) { throw Lang.wrapThrow(e); } }
/** * 拷贝一个目录 * * @param src * 原始目录 * @param target * 新目录 * @return 是否拷贝成功 * @throws IOException */ public static boolean copyDir(File src, File target) throws IOException { if (src == null || target == null || !src.exists()) return false; if (!src.isDirectory()) throw new IOException(src.getAbsolutePath() + " should be a directory!"); if (!target.exists()) if (!makeDir(target)) return false; boolean re = true; File[] files = src.listFiles(); if (null != files) { for (File f : files) { if (f.isFile()) re &= copyFile(f, new File(target.getAbsolutePath() + "/" + f.getName())); else re &= copyDir(f, new File(target.getAbsolutePath() + "/" + f.getName())); } } return re; }
/** * 自动决定是 copy 文件还是目录 * * @param src * 源 * @param target * 目标 * @return 是否 copy 成功 */ public static boolean copy(File src, File target) { try { if (src.isDirectory()) return copyDir(src, target); return copyFile(src, target); } catch (IOException e) { throw Lang.wrapThrow(e); } }