如何将文件复制到Java 7中的目录
问题描述:
我正在尝试使用路径和文件将大量文件复制到Java 7中的输出目录。这不起作用:
I'm trying to copy a number of files to an output directory in Java 7 using Path and Files. This doesn't work:
Files.copy(Paths.get("/my/file.txt"), Paths.get("/my/output/directory/");
它生成一个目录不为空错误。
It generates a "directory not empty" error.
是的,我可以编写代码来直接命名输出文件,或者使用Guava,但我试图用最简单的方法使用新的Java 7 nio课程。
Yes, I could write code to name the output file directly, or use Guava, but I'm trying to do it the simplest way using the new Java 7 nio classes.
答
最简单的方法:
Path file = /* path to source file */
Path to = /* path to destination directory */
Files.copy(file, to.resolve(file.getFileName()));