Java Files.copy() 不复制文件

Kev*_*gem 5 java

我制作了这种方法,可以将文件从一个绝对路径(输入目录)复制到另一个绝对路径(输出目录)。

它没有给我任何错误,但是没有文件被复制到输出文件夹。

为什么会这样?

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;
}
Run Code Online (Sandbox Code Playgroud)

Kev*_*gem 4

正如@zapl所说,Files.copy()仅复制目录。

我通过导入 Apache commons.io 库找到了解决方案。

org.apache.commons.io.FileUtils.copyDirectory(new File(input), new File(output));
Run Code Online (Sandbox Code Playgroud)

这有效。