Search code examples
javafile-copyingapache-commons-io

Java copy directory slow


I have several folders of size >2.5GB on C drive which is SSD. Through Java, I'm moving these folders to another shared drive which also happens to SSD using FileUtils.copyDirectoryToDirectory(sourceDir, destiDir);

It works fine but is slow (taking ~30 mins) when compared to windows default move option which takes 5 mins. I googled around to see if there is a better way to increase the performance of moving directories through my java program but no luck. Can someone suggest me the best way to move these directories?


Solution

  • ok this is what I did

    Used a robocopy command within java to copy directories between two locations. Tested with a ~9GB file and was able to copy in ~9 mins. Below is the code snippet

     String sourceFolder = new File("C:\\test\\robocopytest\\source\\20170925T213857460").toString();
            String destFolder = new File("C:\\test\\robocopytest\\destination\\20170925T213857460").toString();
            StringBuffer rbCmd = new StringBuffer();
            if ((sourceFolder != null) && (destFolder != null))
            {
              if (sourceFolder.contains(" ")) {
                if (sourceFolder.startsWith("\\")) {
                  sourceFolder = "/\"" + sourceFolder.substring(1) + "/\"";
                } else {
                  sourceFolder = "\"" + sourceFolder + "\"";
                }
              }
              if (destFolder.contains(" ")) {
                if (destFolder.startsWith("\\")) {
                  destFolder = "/\"" + destFolder.substring(1) + "/\"";
                } else {
                  destFolder = "\"" + destFolder + "\"";
                }
              }
              rbCmd.append("robocopy " + sourceFolder + " " + destFolder);
              Process p = Runtime.getRuntime().exec(rbCmd.toString());
            }