Merging 2 folders in java keeping the common files by changing its name -
there 15 folders have. every folder has similar sub-structure. merge contents of folders: 1. taking 2 folders @ time 2. taking 3 folders @ time such both files same name saved.
example:
folder 1 ---pqr ------abc.txt ------xyz.txt folder 2 ---pqr ------abc.txt
after merging:
folder 3 ---pqr -------abc.txt -------abc.txt(2) -------xyz.txt
please me java code. in advance.
this have done far in netbeans.
package merger; import org.apache.commons.io.fileutils; import java.io.file; import java.io.ioexception; import java.util.hashmap; import java.util.map; public class merger { public static void main(string[] args) { } public void copydifferentfolderfilesintoone(string mergedfolderstr, string... foldersstr) throws ioexception { final file mergedfolder = new file(mergedfolderstr); final map<string, file> filesmap = new hashmap<> (); (string folder : foldersstr) { updatefilesmap(new file (folder), filesmap, null); } (final map.entry<string, file> fileentry : filesmap.entryset()) { final string relativename = fileentry.getkey(); final file srcfile = fileentry.getvalue(); fileutils.copyfile (srcfile, new file (mergedfolder, relativename)); } } private void updatefilesmap(final file basefolder, final map<string, file> filesmap, final string relativename) { (final file file : basefolder.listfiles()) { final string filerelativename = getfilerelativename (relativename, file.getname()); if (file.isdirectory()) { updatefilesmap(file, filesmap, filerelativename); } else { final file existingfile = filesmap.get (filerelativename); if (existingfile == null || file.lastmodified() > existingfile.lastmodified() ) { filesmap.put (filerelativename, file); } } } } private string getfilerelativename(final string basename, final string filename) { return basename == null ? filename : basename + "/" + filename; } }
Comments
Post a Comment