@Test public void copyDir() throws IOException { long stamp = System.currentTimeMillis(); File from = new File("/tmp/" + stamp); File to = new File("/tmp/copyOf" + stamp); final File fromTmpDir = new File(from.getPath() + File.separator + "recur"); fromTmpDir.mkdirs(); File tmp = File.createTempFile("one", ".txt", from); String content = "testing" + System.currentTimeMillis(); FileWriter writer = new FileWriter(tmp); writer.write(content); writer.close(); File tmp2 = File.createTempFile("two", ".txt", fromTmpDir); writer = new FileWriter(tmp2); writer.write(content + "two"); writer.close(); FileUtil.copyDir(from, to); to = new File("/tmp/copyOf" + stamp); File[] toFiles = to.listFiles(); assertEquals(2, toFiles.length); for (File subFile : toFiles) { if (subFile.isDirectory()) { File[] subToFiles = subFile.listFiles(); assertEquals(1, subToFiles.length); FileInputStream stream = new FileInputStream(subToFiles[0]); FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); String copied = Charset.defaultCharset().decode(bb).toString(); stream.close(); assertEquals(content + "two", copied); } else { FileInputStream stream = new FileInputStream(subFile); FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); String copied = Charset.defaultCharset().decode(bb).toString(); stream.close(); assertEquals(content, copied); } } to = new File("/tmp/copyOf" + stamp + stamp); FileUtil.copyDir(from, to, new FilenameFilter() { @Override public boolean accept(File dir, String name) { return (name.equals(fromTmpDir.getName())); } }); to = new File("/tmp/copyOf" + stamp + stamp); toFiles = to.listFiles(); assertEquals(1, toFiles.length); for (File subFile : toFiles) { FileInputStream stream = new FileInputStream(subFile); FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); String copied = Charset.defaultCharset().decode(bb).toString(); stream.close(); assertEquals(content, copied); } }
public static boolean copyDir(File fromDir, File toDir) { return copyDir(fromDir, toDir, null); }
FileUtil { public static boolean copyDir(File fromDir, File toDir) { return copyDir(fromDir, toDir, null); } }
FileUtil { public static boolean copyDir(File fromDir, File toDir) { return copyDir(fromDir, toDir, null); } private  FileUtil(); }
FileUtil { public static boolean copyDir(File fromDir, File toDir) { return copyDir(fromDir, toDir, null); } private  FileUtil(); static boolean copyDir(File fromDir, File toDir); static boolean copyDir(File fromDir, File toDir, FilenameFilter excluding); static boolean copy(File from, File to); static boolean copy(URL from, File to); static boolean copy(InputStream from, File to); static String getLocalPath(URL url, Map<String, String> headers, String name, String intoName); static boolean download(URL remoteUrl, Map<String, String> headers, File into, String name, String intoName, boolean ignoreFNF); static URL getUrl(String path); static InputStream downloadToStream(URL remoteUrl, Map<String, String> headers); static String pathFromParts(String ... parts); static File fromParts(String ... parts); static String resolveUnixTilde(String path); static String reverseUnixTilde(String path); static boolean isLocalPath(String path); static String encodeForURI(String filePath); static void delete(File file); static String getCanonicalPath(File file); static String stripFileUriPrefix(String path); static String getSha1Hash(File file); }
FileUtil { public static boolean copyDir(File fromDir, File toDir) { return copyDir(fromDir, toDir, null); } private  FileUtil(); static boolean copyDir(File fromDir, File toDir); static boolean copyDir(File fromDir, File toDir, FilenameFilter excluding); static boolean copy(File from, File to); static boolean copy(URL from, File to); static boolean copy(InputStream from, File to); static String getLocalPath(URL url, Map<String, String> headers, String name, String intoName); static boolean download(URL remoteUrl, Map<String, String> headers, File into, String name, String intoName, boolean ignoreFNF); static URL getUrl(String path); static InputStream downloadToStream(URL remoteUrl, Map<String, String> headers); static String pathFromParts(String ... parts); static File fromParts(String ... parts); static String resolveUnixTilde(String path); static String reverseUnixTilde(String path); static boolean isLocalPath(String path); static String encodeForURI(String filePath); static void delete(File file); static String getCanonicalPath(File file); static String stripFileUriPrefix(String path); static String getSha1Hash(File file);  }