-1

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

Axel Werner
  • 186
  • 1
  • 17
  • 1
    You can use the Apache Commons library: [`FileUtils.copyDirectory()`](https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#copyDirectory(java.io.File,%20java.io.File)). – Phylogenesis Oct 02 '15 at 10:51
  • 1
    i think someone misunderstood me. if there is a "single line" solution for JAVA it would be just the thing i am looking for, but not yet found. - anything i found requires A LOT more to add to my code to get the job done. so i would be happy if you can show me what "one liner" would do the job with java. - btw... Apache FileUtils seems to NOT doing the job as asked. they copy a dir to a dir. but NOT the content of that dir to a dir. also they seem not to support recursion and globbing without plenty additional preparation and filtering. :/ – Axel Werner Oct 02 '15 at 10:52
  • @AxelWerner please, look at my answer.... you are misunderstoonding what means `cp`.... – Jordi Castilla Oct 02 '15 at 10:56
  • 2
    Globbing isn't handled by `cp` either. It's handled by the shell. – Phylogenesis Oct 02 '15 at 10:59

3 Answers3

1

Here is a one statement Java solution:

Runtime.exec(new String[] {"sh", "-c",
                           "cp -a \"" + src + ""/* \"" + target + "\""});

Obviously not portable, but there is no reinvention of wheels here.

The trick is to let the shell handle the wildcard expansion for you.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

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.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • oh i totally missed that point looking through the JavaDocs. THANKS! - But they dont say much about recursion (if subdirectories are included or not). im going to test that. – Axel Werner Oct 02 '15 at 12:11
  • this worked for me! its a pitty that the method name does not imply that this method does it slightly different from all the others. i lissed that point while reading through the javaDocs of this lib . thanks for the hint! – Axel Werner Oct 09 '15 at 06:14
0

I searched the net up and down and found PLENTY of "solutions" to this using Oracles and/or Apaches FileUtils etc. 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.

My initial thought is...

...........................................

Why you say ApacheCommons.FileUtils is 20+ lines of code??

copyDirectoryToDirectory(File srcDir, File destDir) API says :

Copies a directory to within another directory preserving the file dates.

This method copies the source directory and all its contents to a directory of the same name in the specified destination directory.

FileUtils.copyDirectoryToDirectory(new File(folder_source), new File(folder_destiny));

But they all require sort of "reinventing the wheel" and are 20+ Lines of code

Did you check how long is cp command source code?

Here it is: cp.c from www.gnu.org there are more than 1000 lines of code.

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • 1
    I agree with your initial answer. My reaction was this: http://i.imgur.com/FDWyFSD.gif – Tschallacka Oct 02 '15 at 10:51
  • i read the JavaDocs for FileUtils.copyFile and copyDir and as far i understood it , it does copy "one file to file" or "one dir to dir" only, no support for wildcards nor recursive usage, or am i totally wrong ??! – Axel Werner Oct 02 '15 at 11:01
  • if it copies the full directory copies recursively, no wildcards but `FileFilter`. – Jordi Castilla Oct 02 '15 at 11:02
  • `FileUtils.copyDirectory()` is recursive. It says so in the documentation. – Phylogenesis Oct 02 '15 at 11:02
  • it also says that its going to copy the "sourceDir" (including all the content) to the targetDir, NOT **just the "content"** of the "SourceDir". so to my eye its not the same. – Axel Werner Oct 02 '15 at 11:06
  • 1
    Don't violate copyright. Posting part of the source code doesn't prove how many lines of code it is anyway. A mere line count would have sufficed. Not that this really answers the question anyway. – user207421 Oct 02 '15 at 11:07
  • Talking about lines of code of libraries is pointless. What is important is that THERE IS at least one library that resolves the problem. – pkalinow Oct 02 '15 at 11:10
  • and its important because OP is complaining of 20+ lines in ApacheCommon libraries – Jordi Castilla Oct 02 '15 at 11:11
  • 1
    I understood it differently. OP complained that applying FileUtils needed 20+ LOC. Which is obviously not true, because there is a one-line solution, like in your answer. – pkalinow Oct 02 '15 at 11:15
  • i think you are still missing my point/missunderstanding me. im not complaining about Apaches 20+ Code. im talking about "how to have the job done within 2-3 lines of code" as it would be possible if i just so a Runtime.getRuntime().exec("xcopy /s /e %source% %dest%"); . – Axel Werner Oct 02 '15 at 11:21
  • if you use runtime you limit your app to a single OS, also, used in the correct way, apache commons is a one(maybe 2)liner – Jordi Castilla Oct 02 '15 at 11:23