FileIOUtils { public static void copyDirectoryAndOverwriteFilesIfNeeded(File srcDir, File destDir) throws IllegalArgumentException, IOException { if(srcDir==null || destDir==null){ throw new IllegalArgumentException("Null inputs"); } if(!srcDir.exists()){ throw new IllegalArgumentException("Source folder does not exist: "+srcDir.getAbsolutePath()); } recursiveCopy(srcDir,destDir); }  static List<String> readFile(File file); static void writeFile(String content, String fileName); static void writeFile(String content, File file); static void writeXML(Object data, String fileName); @SuppressWarnings("unchecked") static T readXML(String fileName); static List<File> getRecursivelyAllFilesInAllSubfolders(File folder, String suffix); static List<File> getRecursivelyAllFilesInAllSubfolders(String base, String suffix); static void copyDirectoryAndOverwriteFilesIfNeeded(File srcDir, File destDir);  }
@Test public void testCopyDirectoryAndOverwriteFilesIfNeeded() throws Exception { File tmpRoot = File.createTempFile("foo"+System.currentTimeMillis(),""); tmpRoot.delete(); tmpRoot.mkdirs(); tmpRoot.deleteOnExit(); File src = new File(tmpRoot,"src"); src.mkdirs(); src.deleteOnExit(); File a = new File(src,"a.txt"); a.deleteOnExit(); a.createNewFile(); File b = new File(src,"b.txt"); b.deleteOnExit(); b.createNewFile(); File folder = new File(src,"folder"); folder.mkdirs(); folder.deleteOnExit(); File c = new File(folder,"c.txt"); c.deleteOnExit(); c.createNewFile(); File dest = new File(tmpRoot,"dest"); File destA = new File(dest,"a.txt"); File destB = new File(dest,"b.txt"); File destC = new File(new File(dest,"folder"),"c.txt"); Assert.assertFalse(destA.exists()); Assert.assertFalse(destB.exists()); Assert.assertFalse(destC.exists()); FileIOUtils.copyDirectoryAndOverwriteFilesIfNeeded(src, dest); Assert.assertTrue(destA.exists()); Assert.assertTrue(destB.exists()); Assert.assertTrue(destC.exists()); Assert.assertEquals(a.lastModified(), destA.lastModified()); Assert.assertEquals(b.lastModified(), destB.lastModified()); Assert.assertEquals(c.lastModified(), destC.lastModified()); Thread.sleep(1000); boolean deleted = c.delete(); Assert.assertTrue(deleted); c.createNewFile(); c.deleteOnExit(); FileWriter out = new FileWriter(c); String line = "foo bar"; out.write(line+"\n"); out.close(); Assert.assertNotEquals(c.lastModified(), destC.lastModified()); FileIOUtils.copyDirectoryAndOverwriteFilesIfNeeded(src, dest); Assert.assertEquals(c.lastModified(), destC.lastModified()); Scanner in = new Scanner(new FileReader(destC)); String read = in.nextLine(); in.close(); Assert.assertEquals(line,read); }