Home > OS >  Merge directories - Java
Merge directories - Java

Time:09-21

I have a directory:

| content
| - folderWithTextFile
| - - textFile.txt
| - - folderWithPdfFile
| - - - pdfFile.pdf

... and I want to merge it into another directory of the same name:

| content
| - folderWithExeFile
| - - exeFile.exe
| - - folderWithPngFile
| - - - pngFile.png

... so that I have this in the end:

| content
| - folderWithTextFile
| - - textFile.txt
| - - folderWithPdfFile
| - - - pdfFile.pdf
| - folderWithExeFile
| - - exeFile.exe
| - - folderWithPngFile
| - - - pngFile.png

Using Files.move(target, destination, StandardCopyOptions.REPLACE_EXISTING) gives me a DirectoryNotEmptyException as secondFolder is not empty.

Using Apache Commons IO FileUtils.moveDirectory(target, destination) gives a FileExistsException as the target already exists.

I would like a way to merge these two structures. It must copy the whole structure, not just one file.

CodePudding user response:

This is a method for merging two directories. The directory of second parameter will be moved to the first one, including files and directories.

import java.io.File;
         
        public class MergeTwoDirectories {
            public static void main(String[] args){
                String sourceDir1Path = "/home/folderWithTextFile/textFile.txt";
                String sourceDir2Path = "/home/folderWithTextFile/exeFile.exe";
         
                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!");
                }
            }
        }

CodePudding user response:

I have come up with this solution:

private static void moveDirectory(File parentFrom, File parentTo) {
        log("Moving "   parentFrom.toPath()   " to "   parentTo);

        for (File file : parentFrom.listFiles()) {

            // Is a regular file?
            if (!file.isDirectory()) { // Is a regular file
                File newName = new File(parentTo, file.getName());
                file.renameTo(newName);
                log("Moved "   file.getAbsolutePath()   " to "   newName.getAbsolutePath());
            } else { // Is a directory
                File newName = new File(parentTo, file.getName());
                newName.mkdirs();
                log("Moving dir "   file.getAbsolutePath()   " to "   newName.getAbsolutePath());
                moveDirectory(file, newName);
            }

            file.delete();
        }

        parentFrom.delete();
    }

It recursively copies the contents of the input file to the output file, and cleans up the input. It is inefficient and definitely sub-ideal, but it works in my circumstances.

  • Related