im still relatively new to java but i have experienve in scripting like DOS, Windows and Bash. today i would like to EASILY copy recursively the content of an directory (files and directories) from a sourceDir to a destinationDir from my Java CLI App.
i searched the net up and down and found PLENTY of "solutions" to this using Oracles and/or Apaches FileUtils etc. But they all require sort of "reinventing the wheel" and are 20+ Lines of code, handling each and every file and dir separately with great afford for something that on the command line shell is done by a SINGLE LINE.
For both on Windows and linux its usually no more than a simple...
cp -a "$sourceDir"/* "$targetDir" # on linux
or
xcopy /s /e %srcdir%\* %trgtdir% # on windows
Yet I was unable to find a prepared library or tool for java that does just that like xcopy/robocopy or cp on bash without adding my a whole new "copy" Class to my app :/ .
Is there a good reason why i should "re-invent the wheel" and no just do some sort of "external shell execution" to call one of those command line tools to have the job done within 2-3 Lines of Code?
Thanks for any Advice and Explanation. Axel
Apache Commons I/O has a method that does this, you need to use the three-argument version of FileUtils.copyDirectory
rather than the two-argument version (which copies the directory itself rather than its contents):
public static void copyDirectory(File srcDir, File destDir, boolean preserveFileDate) throws IOException
This method copies the contents of the specified source directory to within the specified destination directory.