Skip to content
Advertisement

Need to copy all files from one folder to another using java

Src folder C:temp
Dest folder C:temp1

temp has one file (sample.csv)and temp1 folder is empty

I need to copy sample.csv to temp1

Note: I cannot specify sample.csv anywhere in code as this name is dynamic.

Path temp = Files.move(paths.get(src),patha.get(dest)).

This is working only if I give dummy file inside dest directory.
C:temp1dummy.csv (i want to specify only C:temp1 and src folder should go inside temp1)

Advertisement

Answer

Apache commons-io library has a FileUtils.copyDirectory() method. It copies all files from source dir and its subfolders to dest dir, creating dest dir if necessary.

FileUtils docs

If you are using Gradle, add this to your dependencies section to add this library to your project:

implementation 'commons-io:commons-io:2.11.0'
Advertisement