Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Files.copy() not copying files

Tags:

java

I've made this method that copies files from one absolute path (input directory) to another absolute path (output directory).

It doesn't give me any error, however no files are copied to the output folder.

Why would this be?

public static boolean copyFiles(String input, String output)
{
    File source = new File(input);
    File dest = new File(output);
    try {
        Files.copy(Paths.get(input), Paths.get(output), StandardCopyOption.REPLACE_EXISTING);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}
like image 762
Kevin Van Ryckegem Avatar asked Sep 25 '22 04:09

Kevin Van Ryckegem


1 Answers

As @zapl said, Files.copy() only copies the directory.

I found the solution, by importing the Apache commons.io library.

org.apache.commons.io.FileUtils.copyDirectory(new File(input), new File(output));

This works.

like image 63
Kevin Van Ryckegem Avatar answered Oct 11 '22 04:10

Kevin Van Ryckegem