如何将文件复制到Java 7中的目录 [英] How to copy a file to a directory in Java 7

查看:86
本文介绍了如何将文件复制到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()));

这篇关于如何将文件复制到Java 7中的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆