This is a method I write for merging two directories. The directory of second parameter will be moved to the first one, including files and directories.
package com.programcreek; import java.io.File; public class MergeTwoDirectories { public static void main(String[] args){ String sourceDir1Path = "/home/programcreek/Desktop/d1"; String sourceDir2Path = "/home/programcreek/Desktop/d2"; File dir1 = new File(sourceDir1Path); File dir2 = new File(sourceDir2Path); mergeTwoDirectories(dir1, dir2); } public static void mergeTwoDirectories(File dir1, File dir2){ String targetDirPath = dir1.getAbsolutePath(); File[] files = dir2.listFiles(); for (File file : files) { file.renameTo(new File(targetDirPath+File.separator+file.getName())); System.out.println(file.getName() + " is moved!"); } } } |
Output:
f1 is moved!
f2 is moved!
testdir is moved!
f2 is moved!
testdir is moved!